1.Two Sum
1.Two Sum
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].
题解一:
采用哈希表。判断两个数之合是否是target------xi+xj=target=>xi=target-xj,我们可以将问题转化为找一个数是否在数组中。
代码:
python
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution(object): 2 def twoSum(self, nums, target): 3 """ 4 :type nums: List[int] 5 :type target: int 6 :rtype: List[int] 7 """ 8 hashdict={} 9 10 for i,item in enumerate(nums): 11 if(target-item) in hashdict: 12 return (hashdict[target-item],i) 13 hashdict[item]=i 14 return []
C++
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 vector<int>result; 5 int length=nums.size(); 6 if(length<2)return result; 7 //创建哈希表 8 unordered_map<int,int>hash(length); 9 10 for(size_t i(0);i<length;i++){ 11 if(hash.find(target-nums[i])!=hash.end()){ 12 result.push_back(hash[target-nums[i]]); 13 result.push_back(i); 14 return result; 15 } 16 else{ 17 hash[nums[i]]=i; 18 } 19 } 20 return result; 21 } 22 };
题解二 --- 排序后使用两个指针
先对数组进行排序,然后使用两个指针分别指向首尾元素,逐步向中间靠拢,直至找到满足条件的索引。
代码:
C++
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 /* 4 * @param numbers : An array of Integer 5 * @param target : target = numbers[index1] + numbers[index2] 6 * @return : [index1+1, index2+1] (index1 < index2) 7 */ 8 vector<int> twoSum(vector<int> &nums, int target) { 9 vector<int> result; 10 const int length = nums.size(); 11 if (0 == length) { 12 return result; 13 } 14 15 // first num, second is index 16 vector<pair<int, int> > num_index(length); 17 // map num value and index 18 for (int i = 0; i != length; ++i) { 19 num_index[i].first = nums[i]; 20 num_index[i].second = i + 1; 21 } 22 23 sort(num_index.begin(), num_index.end()); 24 int start = 0, end = length - 1; 25 while (start < end) { 26 if (num_index[start].first + num_index[end].first > target) { 27 --end; 28 } else if(num_index[start].first + num_index[end].first == target) { 29 int min_index = min(num_index[start].second, num_index[end].second); 30 int max_index = max(num_index[start].second, num_index[end].second); 31 result.push_back(min_index); 32 result.push_back(max_index); 33 return result; 34 } else { 35 ++start; 36 } 37 } 38 39 return result; 40 } 41 };
源码分析
- 异常处理。
- 使用
length
保存数组的长度,避免反复调用nums.size()
造成性能损失。 - 使用
pair
组合排序前的值和索引,避免排序后找不到原有索引信息。 -
使用标准库函数排序。
复杂度分析
遍历一次原数组得到pair
类型的新数组,时间复杂度为 O(n),空间复杂度也为 O(n),标准库中的排序方法时间复杂度近似为 O(nlogn)两根指针遍历数组时间复杂度为 O(n)