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

Two Sum

时间:2014-11-08 16:40:21      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   使用   sp   strong   

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

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int> &numbers, int target) {
 4         bool find = false;
 5         int index1,index2;
 6         auto it = numbers.begin();
 7         auto tmp = it;
 8         while(!find && it != numbers.end() ){
 9             tmp = it+1;
10             while(!find && tmp!=numbers.end()  ){
11                 if(*it+*tmp ==target){
12                     find = true;
13                     index2 = tmp - numbers.begin()+1;
14                 }
15                 ++tmp;
16             }
17             ++it;
18         }
19         index1 = it-numbers.begin();
20         vector<int> temp;
21         if(it != numbers.end()){
22             temp.push_back(index1);
23             temp.push_back(index2);
24         }
25 
26         return temp;
27     }
28 };

这段代码时间复杂度为O(n^2),,在本地测试通过,但是oj不能ac,错误信息:Time Limit Exceeded超时了。

使用sort (algorithm)函数 先对 numbers的拷贝 排序(O(NlogN)),找出 add number ,存放在 vector<int>中。

在走一遍 vector<int> numbers (O(N)),记下索引,有些小细节注意一下就OK了。

 

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> index;
        vector<int> copy(numbers);
        sort(copy.begin(),copy.end());//O(nlogn)
        vector<int> returnnu(SumAlgorithm(copy,target));
        int i=0,j=0,flag=-1;//flag 用于标记 index里边是否有数了,防止 两数一样,如果flag初始化0 ,会出错。
        while(j<2 && !returnnu.empty() && i<= numbers.size()-1){
            if(i != flag && returnnu[j] == numbers[i]){
                index.push_back(1+i);
                flag = i;
                i=-1;//非常重要,纠结这里好久了,numbers没排序,i需要置0
                ++j;
            }
            ++i;
        }
        if( index[0] > index[1]){
            index[1] = index[1]^index[0];
            index[0] = index[0]^index[1];
            index[1] = index[1]^index[0];
        }
        return index;
    }
    //返回 两个 add的数 ,有序
    vector<int> SumAlgorithm(vector<int>& numbers,int target){
        vector<int> tmp;
        int j = numbers.size()-1;
        int i=0,sum =0;
        bool find = false;
        while(!find && i<j){
            sum = numbers[i] + numbers[j];
            if(sum == target)
                find = true;
            else if(sum < target)
                ++i;
            else
                --j;
        }
        if(find){
            tmp.push_back(numbers[i]);
            tmp.push_back(numbers[j]);
        }
        return tmp;   
    }
};

 

Two Sum

标签:style   blog   io   color   ar   os   使用   sp   strong   

原文地址:http://www.cnblogs.com/ittinybird/p/4083524.html

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