题目描述: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
这道题可以有以下的3种解法:
1. 两层循环,暴力求解。复杂度是O(n2),测试不通过;
2. hash表,已知第一个数,计算出第二个数,存在则找到。复杂度是O(n);
3. 先排序,然后左右加逼。排序是O(n logn),左右加逼是O(n),综合是O(n logn)。
1. 两层循环,暴力求解
class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { int i, j, sum; vector<int> results; for(i=0; i<numbers.size()-1; i++){ for(j=i+1; j<numbers.size(); j++){ if( numbers[i] + numbers[j] == target ){ results.push_back(i+1); results.push_back(j+1); break; } } } return results; } };
2. hash表
class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { unordered_map<int, int> mapping; vector<int> result; for (int i = 0; i < numbers.size(); i++){ mapping[numbers[i]] = i; } for (int i = 0; i < numbers.size(); i++){ int gap = target - numbers[i]; if(mapping.find(gap) != mapping.end()) //mapping中没有找到相应主键,就返回map.end() if( i != mapping[gap]){ //同一个数不能加两次 result.push_back(i + 1); result.push_back(mapping[gap] + 1); break; } } return result; } };
3. 先排序,然后左右加逼
struct Node{ int num; //numbers[] int pos; //索引 }; bool cmp(Node a, Node b){ return a.num < b.num; } class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { int i, j; vector<int> result; vector<Node> array; for(i = 0; i < numbers.size(); i++){ Node temp; temp.num = numbers[i]; temp.pos = i; array.push_back(temp); } //排序 sort(array.begin(), array.end(), cmp); //左右加逼 for(i = 0, j = numbers.size()-1; i != j; ){ int sum = array[i].num + array[j].num; if(sum == target){ if(array[i].pos < array[j].pos){ result.push_back(array[i].pos + 1); result.push_back(array[j].pos + 1); }else{ result.push_back(array[j].pos + 1); result.push_back(array[i].pos + 1); } break; } else if(sum < target) i++; else if(sum > target) j--; } return result; } };
Java:
/* * 数组有序 仅显示唯一结果 用指针分别指向两端 若数组无序,采用hashmap */ public static int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> Map = new HashMap<Integer, Integer>(); int[] result = new int[2]; // key-value,将数组对应下标存起来 for (int i = 0; i < nums.length; i++) { Map.put(nums[i], i); } for (int i = 0; i < nums.length; i++) { int gap = target - nums[i]; // 如果存在gap,且不是自身,就返回 if (Map.containsKey(gap)) { if (i != Map.get(gap)) { result[0] = i + 1; result[1] = Map.get(gap) + 1; break; } } } return result; }
知识点总结:
1. Sort函数
Sort函数有三个参数:
(1)第一个是要排序的数组的起始地址。
(2)第二个是结束的地址(最后一位要排序的地址)
(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。
Sort函数使用模板:
Sort(start,end,排序方法)