-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001-TwoSum.cpp
More file actions
42 lines (35 loc) · 909 Bytes
/
001-TwoSum.cpp
File metadata and controls
42 lines (35 loc) · 909 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 16ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "001-TwoSum.h"
namespace LeetCode
{
_001_TwoSum::_001_TwoSum()
{
}
_001_TwoSum::~_001_TwoSum()
{
}
vector<int> _001_TwoSum::twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> restIndex;
vector<int> result;
for (int i = 0; i < nums.size(); i++)
{
if (restIndex.find(nums[i]) == restIndex.end())
{
restIndex[target - nums[i]] = i;
}
else
{
result.push_back(restIndex[nums[i]] + 1);
result.push_back(i + 1);
break;
}
}
return result;
}
}