two Sum ---- LeetCode 001

Posted on 2016-03-29 22:52  徐岩  阅读(146)  评论(0)    收藏  举报

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution 1: 

 1 class Solution 
 2 {
 3 public:
 4     vector<int> twoSum(vector<int>& nums, int target) 
 5     {
 6         unordered_map<int, int> myMap;
 7         vector<int> result;
 8         
 9         for(size_t i = 0; i < nums.size(); ++i)
10         {
11             myMap[nums[i]] = i;
12         }
13             
14         for(size_t i = 0; i < nums.size(); ++i)
15         {
16             const int gap = target - nums[i];
17             auto it = myMap.find(gap);
18             if(it != myMap.end() && it->second != i)
19             {
20                 result.push_back(i);
21                 result.push_back(myMap[gap]);
22                 break;  // Assume that each input would have 
23                         // exactly one solution
24             }
25         }
26         return result;
27     }
28 };
View Code

Solution 2:  暴力查找,超时

 1 class Solution 
 2 {
 3 public:
 4     vector<int> twoSum(vector<int>& nums, int target) 
 5     {
 6         vector<int> result;
 7         for(size_t i = 0; i < nums.size(); ++i)
 8         {
 9             for(size_t j = i + 1; j < nums.size(); ++j)
10             {
11                 if(nums[i] + nums[j] == target)
12                 {
13                     result.push_back(i);
14                     result.push_back(j);
15                     break;
16                 }
17             }
18         }
19         return result;
20     }
21 };
View Code

Solution 3:  先排序,然后左右夹逼

 1 class Solution
 2 {
 3 public:
 4     vector<int> twoSum(vector<int>& nums, int target)
 5     {
 6         vector<int> result;
 7         vector<Node> num;
 8         for(size_t i = 0; i < nums.size(); ++i)
 9         {
10             Node temp;
11             temp.value = nums[i];
12             temp.pos = i;
13             num.push_back(temp);
14         }
15         sort(num.begin(), num.end(), cmp);
16         for(size_t i = 0, j = num.size() - 1; i != j; )
17         {
18             int sum = num[i].value + num[j].value;
19             if(sum == target)
20             {
21                 int smallPos = min(num[i].pos, num[j].pos);
22                 int largePos = max(num[i].pos, num[j].pos);
23                 result.push_back(smallPos);
24                 result.push_back(largePos);
25                 break;  // 找到解后,中断
26             }
27             else if(sum > target) --j;
28             else ++i;
29         }
30         return result;
31     }
32 private:
33     struct Node
34     {
35         int value;
36         int pos;
37     };
38     static bool cmp(const Node &a, const Node &b)
39     {
40         return a.value < b.value;
41     }
42 };
View Code