明明的leetcode日常:1. Two Sum

最近一直在科研科研科研,好久没有刷题手生了,我又不愿意给我的mac装vs所以只好在vim里盲写。leetcode第一题都做了一小时【晕。

题干:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

我的思路:排序,然后头尾安插迭代器,往中间靠拢,加到合适的为止。这个方法的时间复杂度取决于sort函数排序的效率,时间复杂度估计是nlogn。最后跑下来大概是在54.29%的位置。

class Solution {
public:
    static bool compare(const pair<int,int> a,const pair<int,int> b)
    {
        return a.first>b.first;
    }

    vector<int> twoSum(vector<int>& nums, int target) {
        vector< pair<int,int> > nums2;
        //两个“>”之间注意加空格
        int i=0;
        for(auto data:nums)
        {
            pair<int,int> temp;
            temp=make_pair(data,i);
            nums2.push_back(temp);
            ++i;
        }
        sort(nums2.begin(),nums2.end(),compare);
        vector<int> result;
        auto start_point=nums2.begin();
        auto end_point=nums2.end(); --end_point;
        while(start_point<end_point)
        {
            int sum=(*start_point).first+(*end_point).first;
            if(sum>target) ++start_point;
            else if(sum<target) --end_point;
            else
            {
                if((*start_point).second<(*end_point).second)
                {
                    result.push_back((*start_point).second);
                    result.push_back((*end_point).second);
                }
                else
                {
                    result.push_back((*end_point).second);
                    result.push_back((*start_point).second);
                }
                ++start_point;
                --end_point;
            }
        }
        return result;
    }
};

我的思路变化过程:后来我想有没有更好的方法,我想用map,但是map本质上也是红黑树排序,时间复杂度也不比sort低,而且我一开始的思路是,将容器中所有的元素都存放在map中,然后在map中查找,但是我面临了一个问题:[3,2,3]这种数据怎么处理?
其实我忽略了一个关键的细节:答案唯一,也就是说只有一对数满足要求,找到这一对就没有往下找的必要了。于是,大神的方法是,在map中边插入元素边检索,找到了直接停止,没找到就继续插入继续找。
为了加快速度,大神用unordered_map代替map,因为unordered_map内部是用哈希表实现的,也就是说,散列得好的话,它的时间复杂度是比map低的

大神的方法:

vector<int> twoSum(vector<int> &numbers, int target)
{
    //用哈希键值对查找,键是原先提供的数字,值是数字在原容器中的下标
    unordered_map<int, int> hash;
    vector<int> result;
    for (int i = 0; i < numbers.size(); i++) {
        int numberToFind = target - numbers[i];
        //直接查找之前存储在哈希表中的元素有没有可以与当前元素匹配的

            //如果有这样的元素,由于本题的答案唯一,因此可以直接返回值
        if (hash.find(numberToFind) != hash.end()) {
            result.push_back(hash[numberToFind]);
            result.push_back(i);            
            return result;
        }

            //如果找不到与当前数匹配的键,就将当前数插入哈希表中,等待被后续的数检索
        hash[numbers[i]] = i;
    }
    return result;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值