-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo_Sum.cpp
More file actions
43 lines (35 loc) · 1.01 KB
/
Copy pathTwo_Sum.cpp
File metadata and controls
43 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//解題思路1:遍歷所有vector內元素,找出相加剛好為target的值
#include<vector>
#include<unordered_map>
using namespace std;
class Solution1 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> sol ;
for(int i=0; i<nums.size(); i++){
for(int j=i+1; j<nums.size(); j++){
if(nums[i] + nums[j] == target){
sol.push_back(i);
sol.push_back(j);
break;
}
}
}
return sol;
}
};
//解題思路2:使用hash table快速查找
class Solution2 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash_table;
for(int i=0; i<nums.size(); i++){
int restNum = target - nums[i];
if(hash_table.find(restNum)!= hash_table.end()){
return {i, hash_table[restNum]};
}
hash_table[nums[i]] = i;
}
return {};
}
};