码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 题解代码整理 1-5题

时间:2015-08-04 23:00:26      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

Two Sum

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

题意:给出一串数字,求下标最小的两个数字和等于target,返回下标,要求index1<index2

思路:先把数据按大小排序,i指向最左,last指向最右,若相加<target则i++,否则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 Two Numbers

 

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

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

题意:给出两个链表,输出两个链表的和,链表的每一位只存储一位,若进位则进位到下个链表结点

/**
 * 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->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" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

题意:找出最长连续字串,保证每个字母最多只出现一次

思路:用last标记上一个可用的起始位置,x数组标记每个字母上一次出现的位置,从做到右遍历一遍,每次取最右的可用起始位置

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 Two Sorted Arrays

 

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

题意:给出两个序列,找出两个序列合并起来的中位数,要求时间复杂度log(n+m)

思路:将问题转换为找两个序列合并起来的第K小数,找第K小数,可以把K平分到A,B两个序列中,如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,则去掉这部分,直到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 string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

题意:求长度为1000的最长回文子串

思路:n*n复杂度区间DP,其他算法以后再实现。。。。

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);

    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 题解代码整理 1-5题

标签:

原文地址:http://blog.csdn.net/u011932355/article/details/47275499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!