leetcode TwoSum

-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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值