Leetcode 1-5 Questions of solving code

Source: Internet
Author: User
Tags add numbers

The Sum of

Given an array of integers, find the numbers such that they add up to a specific target number.

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input: numbers={2, 7, one, target=9
Output: index1=1, index2=2

Test instructions: Give a string of numbers, the lowest two digits of the subscript and equal to target, return subscript, require INDEX1<INDEX2

Idea: First the data sorted by size, I point to the leftmost, last point to the right, if the sum <target is i++, otherwise last--

Class Solution{public:    vector<int> twosum (vector<int>& nums, int target)    {        Pair<int, int>a,b;        vector<pair<int,int>>mark;        for (int i=0;i<nums.size (); i++)        {            b={nums[i],i+1};            Mark.push_back (b);        }        Sort (Mark.begin (), Mark.end ());        int Last=mark.size ()-1;                for (int i=0;i<last;)        if (mark[i].first+mark[last].first==target)        {            b={min (mark[i].second,mark[ Last].second), Max (Mark[i].second,mark[last].second)};            break;        }        else         if (mark[i].first+mark[last].first<target)            i++;        else             last--;                            vector<int>ans;        Ans.push_back (B.first);        Ans.push_back (B.second);                return ans;    };

ADD Numbers 

You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order and all of their nodes contain a single digit. ADD the numbers and return it as a linked list.

Input: (2, 4, 3) + (5, 6, 4)
Output: 7, 0, 8

Test instructions: Gives two linked lists, outputs two linked lists, and each bit of the list stores only one bit, and if carry is rounded to the next linked list node

/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *};        */class Solution {public:listnode* addtwonumbers (listnode* L1, listnode* L2) {ListNode *e,*ans,*mark;        int t=0;        Ans= (listnode*) malloc (sizeof (ListNode));        Mark=ans; while (L1!=null | | l2!=null) {if (l1!=null && l2!=null) {e= (ListNode                *) malloc (sizeof (ListNode));                *e=listnode ((l1->val+l2->val+t)%10);                                t= (l1->val+l2->val+t)/10;                l1=l1->next;                l2=l2->next;                mark->next=e;            mark=mark->next;                } if (L1==null && l2!=null) {e= (listnode*) malloc (sizeof (ListNode));                *e=listnode ((l2->val+t)%10);                                t= (l2->val+t)/10; l2=l2-&Gt;next;                mark->next=e;            mark=mark->next;                } if (L1!=null && l2==null) {e= (listnode*) malloc (sizeof (ListNode));                *e=listnode ((l1->val+t)%10);                                t= (l1->val+t)/10;                l1=l1->next;                mark->next=e;            mark=mark->next;            }} if (t!=0) {e= (listnode*) malloc (sizeof (ListNode));            *e=listnode (t);        mark->next=e;                            } return ans->next; }};

Longest Substring without repeating characters 

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.

Test instructions: Find the longest consecutive string to ensure that each letter appears at most once

Idea: With the last mark on the previous available starting position, the x array marks the last occurrence of each letter, from the right to go through it, each time to take the most right available starting position

Class Solution{public:    int lengthoflongestsubstring (string s)     {                int x[220],last=-1,ans=0;        memset (x,-1,sizeof (x));        for (int i=0;i<s.size (); i++)        {            if (x[s[i]]>last) last=x[s[i]];            if (I-last>ans) ans=i-last;            x[s[i]]=i;        }        return ans;            };

Median of Sorted Arrays 

there is sorted arraysnums1 andNUMS2of size M and n respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (M+n)).

Test instructions: Gives two sequences, finds the median of two sequences combined, requires time complexity log (N+M)

Idea: The problem is converted to find the two series combined with the K decimal, find the K decimal, you can divide K into a B two sequence, if a[k/2-1]<b[k/2-1], then a[0]~a[k/2-1] must be in the series of small number of K, then remove this part, until k== 1,.

Class Solution {public:    double findmediansortedarrays (vector<int>& nums1, vector<int>& nums2 )     {        int a[1010],b[1010];        M=nums1.size ();        for (int i=0;i<m;i++)        a[i]=nums1[i];        N=nums2.size ();        for (int i=0;i<n;i++)        b[i]=nums2[i];        if ((n+m)%2==1)            return Find (A,m,b,n, (n+m)/2+1);        else             return (Find (A,m,b,n, (n+m)/2) +find (A,m,b,n, (n+m)/2+1))/2;    } Private:int n,m;    Double find (int a[],int m,int b[],int n,int k)    {        if (m>n)             return find (b,n,a,m,k);        if (m==0)            return b[k-1];        if (k==1)            return min (a[0],b[0]);        int Pa=min (k/2,m), Pb=k-pa;        if (A[pa-1]<b[pb-1])            return find (A+PA,M-PA,B,N,K-PA);        else         if (a[pa-1]>b[pb-1])            return find (A,M,B+PB,N-PB,K-PB);        else             return a[pa-1];}                ;

Longest palindromic Substring

Given A stringS, find the longest palindromic substring inS. Assume that maximum length ofSis, and there exists one unique longest palindromic substring.

Test instructions: To find the longest palindrome string of length 1000

Idea: N*n complexity interval DP, other algorithm later realized ....

Class Solution{public:    string Longestpalindrome (string s)    {        int len=s.length (), mx=1,start=0;        int Flag[len][len];        for (int i=0;i<len;i++) for            (int j=0;j<len;j++)            if (i>=j)                flag[i][j]=1;            else                flag[i][j]=0;        for (int i=2;i<=len;i++) for            (int j=0;j<len-i+1;j++)            {                int k;                k=i+j-1;                if (S[j]==s[k])                    flag[j][k]=flag[j+1][k-1];                if (flag[j][k]==1 && mx<i)                    {                        mx=i;                        start=j;                    }            }        Return S.substr (START,MX);    };



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 1-5 Questions of solving code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.