-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.cpp
38 lines (36 loc) · 819 Bytes
/
1.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
#include"iostream"
#include"vector"
#include"map"
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> ans;
map<int,int> record;
for(int i=0;i<nums.size();i++)
record[nums[i]]=i;
for(int i=0;i<nums.size();i++)
{
if(record.count(target-nums[i]) && record[target-nums[i]]!=i) {
ans.push_back(i);
ans.push_back(record[target-nums[i]]);
break;
}
}
return ans;
}
};
int main()
{
int target=target = 9;
int lis[]={2, 7, 11, 15};
vector<int> nums,ans;
for(int i=0;i<4;i++)
nums.push_back(lis[i]);
ans=Solution().twoSum(nums,target);
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
return 0;
}