leetcode:two sum

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

 

Show Tags
Show Similar Problems
 
解法一:
      采用简单Brute Force算法,从序列一端开始遍历至另一端,寻找满足要求的解。提交之后发现提示“Time Limit Exceeded”,立刻明白题目不会这么简单,毕竟是各大IT企业的笔试题,这道题目如果简单的两重循环遍历时间复杂度是O(N^2),如果array元素个数很多,时间因素将是瓶颈。
 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int> &numbers, int target) {
 4         int i, j, sum;
 5         vector<int> results;
 6         for(i=0; (i<numbers.size()-1); i++){
 7             //if(numbers[i]>target) continue; //这样判断不行,因为可能有负数+正数=target的情况
 8             for(j=i+1; j<numbers.size(); j++){
 9                 //if(numbers[j]>target) continue;
10                 sum=numbers[i]+numbers[j];
11                 if(sum==target){
12                     //cout<<"index1="<<(i+1)<<", index2="<<(j+1)<<endl;
13                     results.push_back(i+1);
14                     results.push_back(j+1);
15                 }
16             }
17         }
18         return results;
19     }
20 };

解法二:

   查过一些资料,许多人是用哈希表实现快速查找的,然后就去恶补哈希原理、C++容器等blabla,第二种解法是参照别人的,其实理解起来不难,下面的方法很好的利用了C++容器map的特性,map是基于红黑树(Red Black Tree)的数据结构,查找时间复杂度O(logN),虽然不及Hash Table的O(1),但C++容器并没有Harsh,所以用好C++函数就能解决大部分问题,但自己能理解最好了。

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int> &numbers, int target) {
 4         int i, sum;
 5         vector<int> results;
 6         map<int, int> hmap;
 7         for(i=0; i<numbers.size(); i++){
 8             if(!hmap.count(numbers[i])){
 9                 hmap.insert(pair<int, int>(numbers[i], i));
10             }
11             if(hmap.count(target-numbers[i])){
12                 int n=hmap[target-numbers[i]];
13                 if(n<i){
14                     results.push_back(n+1);
15                     results.push_back(i+1);
16                     //cout<<n+1<<", "<<i+1<<endl;
17                     return results;
18                 }
19 
20             }
21         }
22         return results;
23     }
24 };

解法三:

     在discuss板块,看到有童鞋share自己15ms的代码,分析其代码主要是用排序sort()方法先让array有序,再遍历找到index1、index2对应的Value(这个遍历比较巧妙,赞!),最后从原来array向量中找出对应的Key值,即index1、index2。时间复杂度主要是排序算法sort(),O(NlogN)。

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int> &numbers, int target) {
 4         vector<int> v = numbers;
 5         sort(v.begin(), v.end());
 6         int left = 0;
 7         int right = (int) v.size() - 1;
 8         int middle;
 9         while (left < right) {
10             int middle = v[left] + v[right];
11             if (middle == target) break;
12             if (middle < target) 
13                 ++left; 
14             else 
15                 --right;
16         }
17 
18         int result_index1;
19         int result_index2;
20         for (int i=0;i<v.size();++i) {
21             if (numbers[i] == v[left]) { 
22                 result_index1 = i; 
23                 break;
24             }
25         }
26 
27         for (int i=(int)v.size()-1;i >= 0;--i) {
28             if (numbers[i] == v[right]) {
29                 result_index2 = i; 
30                 break; 
31             }
32         }
33 
34         if (result_index1 > result_index2) 
35             swap(result_index1,result_index2);
36 
37         vector<int> result {result_index1 + 1, result_index2 + 1};
38         return result;
39     }
40 };

 

posted @ 2015-08-02 17:04  A.ArmStrong  阅读(190)  评论(0编辑  收藏  举报