-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.two_sum.cpp
37 lines (34 loc) · 861 Bytes
/
1.two_sum.cpp
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
//coding:utf-8
/*
Program:
Description:
Shanbo Cheng: [email protected]
Date: 2016-07-06 15:00:26
Last modified: 2016-08-18 13:21:10
GCC version: 4.7.3
*/
//
//in this problem, leetcode #1, this code works, because it guarantees each test case has exactly one solution.
//otherwise, the code should be modified (should be return at line 29)
//
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
unordered_map<int, int> int_map;
public:
vector<int> twoSum(vector<int>& nums, int target) {
int index = 0;
for(auto i: nums) {
if(int_map.find(target - i) == int_map.end())
int_map[i] = index++;
else
return vector<int>{int_map[target- i], index};
}
return vector<int>();
}
};
int main() {
return 0;
}