1. Two Sum

 

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].

 

题目链接:https://leetcode.com/problems/two-sum/description/

解法一:两重循环遍历数组(时间复杂度:O(n²))

解法二:1.用map来存放,键值为数组的值,实值为个数,

    2.遍历数组,用find查找是否存在我们要找的值(target - nums[i]),找到判断是否与nums[i]相等,相等则判断是否有两个同样的值

    3.最后记录下标值的时候,要注意当找到的两个值相等的时候,不能记录为相同的,要记录下一个

    时间复杂度O(nlog(n)),其中O(log(n))为map容器产生

    vector<int> twoSum(vector<int>& nums, int target) {
        int length = nums.size();
        vector<int> vec;
        map<int , int> simap;
        int num = 0;
        for(int i = 0; i < length; i++)
            simap[nums[i]] ++;
        for(int i = 0; i < length; i++)
        {
            num = target - nums[i];
           if(simap.find(num) != simap.end())//找到了
           {
               if(nums[i] == num && simap[num] == 1)//同一个值
                   continue;
               vec.push_back(i);
               break;
           }
        }
        if(vec.size() != 0)//找到了和为target的两个数
        {
            for(int i = 0; i < length; i++)
                if(nums[i] == num)
                {
                    if(vec[0] == i)
                        continue;
                    vec.push_back(i);
                     break;
                }
        }
        return vec;
    }

解法三:1.vec存储pair类型,分别为数组的值和下标,

    2.存储完后用sort进行排序,

    3.用迭代器遍历数组,调用lower_bound函数,查找另一个值(target - ite->first)

    时间复杂度(O(nlog(n)))

    vector<int> twoSum(vector<int>& nums, int target) {
        int length = nums.size();
        vector<pair<int , int> > vec;
        vector<int> res;
        for(int i = 0; i < length; i++)
        {
            vec.push_back(make_pair(nums[i],i));
        }
        sort(vec.begin(),vec.end());
        vector<pair<int , int>>::iterator tmp,i,ite = vec.begin();
        while(ite != vec.end()) 
        {
            auto tm = next(ite, 1);
            i = lower_bound(tm,vec.end(),make_pair(target - (ite->first), 0), [](const pair<int, int> & a, const pair<int, int> & b) -> bool
                                        { 
                                            return a.first < b.first; 
                                        });
            if(i!=vec.end()&&i->first + ite->first == target)
            {
                res.push_back(ite->second);
                res.push_back(i->second);
                break;
            }
            ite ++;
        }
        return res;
    }

 

转载于:https://www.cnblogs.com/Lune-Qiu/p/8367624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值