leetcode TwoSum

来源:互联网 发布:期货软件开发公司 编辑:程序博客网 时间:2024/05/16 17:56

-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

class Solution {public:    vector<int>twoSum(vector<int>& nums, int target) {                 vector<int> result;        for(vector<int>::iterator one =nums.begin();one!=nums.end();one++)            for(vector<int>::iteratortwo=one+1;two!=nums.end();two++)            {                         if(*one + *two ==target)                         {                            result.push_back(one-nums.begin()+1);                             result.push_back(two-nums.begin()+1);                             return result;                         }            }        return result;    }};

以上方法为O(n^2),不能被接受,因此应该将逐个比较变为直接查找。先将数据进行快速排序,时间复杂度为O(N*logN),然后求出target-num[i],进行二分查找(单次平均O(logN),最坏O(N)),最坏时间复杂为(N*logN).

通过采用hashtable来改进,hashtable的存取只需要O(1),将nums[i]数据插入哈希表,然后判断target-nums[i]是否在hashtable内即可,时间复杂度为O(N)

STL中没有hashtable的容器,只能用map实现。Map底层使用红黑树实现,所以查找时间为O(logN),理论上逊于hashtable的O(N).但实际情况下,hashtable计算hash值需要消耗时间,也许比O(logN)更大

class Solution {public:    vector<int>twoSum(vector<int>& nums, int target) {        map<int,int> hmap;        vector<int> result;        int i;        for(i=0;i<nums.size();i++)        {            if(!hmap.count(nums[i]))               hmap.insert(pair<int,int>(nums[i],i));            if(hmap.count(target-nums[i]))            {                int n=hmap[target-nums[i]];                if(n<i)                {                    result.push_back(n+1);                    result.push_back(i+1);                    return result;                }            }        }        return result;    }};

这里写图片描述

可以先复制数据,对数据副本排序(O(NlogN),然后用两个指针,begin指向头,end指向尾,然后把两个指针所指数据相加为sum,如果sum< target,begin++,如果sum>target,end–,如果sum==target,则找到了符合要求的两个数,复杂度为O(N),然后分别找出这个两个在未排序前的数据列表中的位置下标,复杂度为O(N)

class Solution {public:    vector<int>twoSum(vector<int>& nums, int target) {        vector<int> result;        vector<int> temp = nums;        int i,n;        sort(temp.begin(),temp.end());        for(i=0,n=temp.size()-1;i<n;)        {            if(temp[i] + temp[n] < target)            {                i++;            }            else if(temp[i] + temp[n] >target)            {                n--;            }            else            {                int index1=0,index2=0,j;                bool flag1=false,flag2=false;                for(j=0;j<nums.size();j++)                {                    if(nums[j] ==temp[i]&&flag1==false)                    {                        index1 = j;                        flag1 = true;                        continue;                    }                   else if(nums[j] ==temp[n]&&flag2==false)                    {                         index2 = j;                        flag2=true;                        continue;                    }                }                if(index1<index2)                {                     index1 = index1^index2;                     index2 = index1^index2;                     index1 = index1^index2;                }                result.push_back(index2+1);                result.push_back(index1+1);                return result;            }        }        return result;    }};

这里写图片描述

0 0
原创粉丝点击