-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathld001.cpp
51 lines (48 loc) · 1.12 KB
/
ld001.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*************************************************************************
> File Name: ld001.cpp
> Author: ihochang
> Mail: [email protected]
> Created Time: Tue Jun 16 17:55:11 2015
************************************************************************/
#include<iostream>
#include<map>
#include<vector>
using namespace std;
class Solution{
public:
map<int,int> saveNum;
int otherNum;
int posNum;
vector<int> result;
vector<int> twoSum(vector<int>& nums, int target){
for (int i = 0;i<nums.size();i++){
saveNum[nums[i]]=i;
}
for (int i =0;i<nums.size()-1;i++){
otherNum = target-saveNum[i];
if (saveNum.find(otherNum)!=saveNum.end()){
if (i!=otherNum){
result.push_back(i+1);
result.push_back(otherNum+1);
break;
}
}
}
return result;
}
};
int main(){
int testNum[] = {2,7,11,15};
int len = sizeof(testNum);
vector <int> nums(testNum,testNum+len);
vector<int> answer;
Solution test;
answer = test.twoSum(nums,9);
cout<<
//for (int i=0;i<1;i++){
// cout<<"index="<<answer[i]<<",";
//}
//
cout<<answer[0]<<answer[1];
return 0;
}