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

1. Two Sum

时间:2018-01-28 11:30:12      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:bre   turn   targe   tin   continue   bsp   cond   ons   push   

 

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(n2))

解法二: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;
    }

 

1. Two Sum

标签:bre   turn   targe   tin   continue   bsp   cond   ons   push   

原文地址:https://www.cnblogs.com/Lune-Qiu/p/8367624.html

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