Two Sum
来源:互联网 发布:黑道圣徒3御姐捏脸数据 编辑:程序博客网 时间:2024/04/29 12:27
初阶。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
思路:
此题需要返回的是下标,不是数字本身。
思路1。暴力,时间复杂度o(n^2)
思路2。用一个hash表,存储每个数对应的下标,复杂度O(n).
class Solution{public:vector<int> twoSum(vector<int> &num,int target){unordered_map<int,int> mapping;vector<int> result;for(unsigned int i=0;i<num.size();i++){mapping[num[i]] = i;}for(unsigned int i=0;i<num.size();i++){const int gap = target - num[i];if (mapping.find(gap)!=mapping.end() && mapping[gap] > i){result.push_back(i+1);result.push_back(mapping[gap]+1);break;}}return result;}};
进阶。3Sum
Given an array S of n integers, are there elements a; b; c in S such that a + b + c = 0? Find all unique
triplets in the array which gives the sum of zero.
Note:
?Elements in a triplet (a; b; c) must be in non-descending order. (ie, a b c)
?The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
思路:先排序,然后左右夹逼。复杂度O(n^2)
class Solution{public:vector<vector<int>> threeNum(vector<int> &num){vector<vector<int>> result;if (num.size() < 3) return result;sort(num.begin(),num.end());int target = 0;vector<int> rec;auto last = num.end();for(auto a = num.begin();a < prev(last,2);a++){auto b = next(a);auto c = prev(last);while(b<c){if(*a + *b + *c < target){++b;}else if(*a + *b + *c > target){--c;}else{//result.push_back({*a, *b, *c});rec.clear();rec.push_back(*a);rec.push_back(*b);rec.push_back(*c);result.push_back(rec);++b;--c;}}}sort(result.begin(),result.end());result.erase(unique(result.begin(),result.end()),result.end());return result;}};
进阶+.3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number,
target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
思路。同上,先排序,然后再左右夹逼。复杂度O(n^2).
进阶++。4Sum
Given an array S of n integers, are there elements a; b; c, and d in S such that a+b+c+d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note:
?Elements in a quadruplet (a; b; c; d) must be in non-descending order. (ie, a b c d)
?The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
思路。
思路1,先排序,然后左右夹逼。复杂度O(n^3).
思路2,用一个hashmap先缓存两个数的和,时间复杂度,平均O(n^2),最坏o(n^4),空间复杂度O(n^2).
思路3,用multimap.时间复杂度O(n^2),空间复杂度O(n^2).
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- 针对项目中同步队列死锁的解决方案
- jquery:serilize使用方法
- quick-x 状态机模式
- Android长按Power键弹出关机Dialog框GlobalActions解析
- Oracle全文检索步骤
- Two Sum
- iostream输入输出流互相绑定问题
- Java中equals()与hashCode()的原理与设计
- iso 获得UIColor中RGB各个元素的值
- 男孩应该具备的十个品质
- Spring4.x+Hibernate4.x整合
- ORA-04031
- Oracle中SQL语句学习五(统计分组语句group by和having)
- ural Binary Lexicographic Sequence (dp + dfs)