Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8194240

Browse files
committed
change directory hierarchy
1 parent 5035f0f commit 8194240

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
File renamed without changes.

‎src/0001_Two_Sum.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <vector>
2+
3+
/*class Solution {
4+
public:
5+
vector<int> twoSum(vector<int>& nums, int target) {
6+
int i, j, i1, j1;
7+
int len;
8+
vector<int> idxs;
9+
10+
len = nums.size();
11+
for (i = 0; i < len; i++){
12+
for (j = i+1; j < len; j++){
13+
if (nums[j] == target - nums[i]){
14+
i1 = i;
15+
j1 = j;
16+
i = len;
17+
break;
18+
}
19+
}
20+
}
21+
22+
idxs.push_back(i1);
23+
idxs.push_back(j1);
24+
25+
return idxs;
26+
}
27+
};*/
28+
29+
class Solution {
30+
public:
31+
vector<int> twoSum(vector<int>& nums, int target) {
32+
int i;
33+
int len;
34+
int res;
35+
std::map<int, int> mymap;
36+
std::map<int, int>::iterator it;
37+
vector<int> idxs;
38+
39+
len = nums.size();
40+
mymap[nums[0]] = 0;
41+
42+
for (i = 1; i < len; i++){
43+
res = target - nums[i];
44+
it = mymap.find(res);
45+
46+
if (it != mymap.end()){
47+
idxs.push_back(mymap[it->first]);
48+
idxs.push_back(i);
49+
mymap.clear();
50+
return idxs;
51+
}
52+
else
53+
mymap[nums[i]] = i;
54+
}
55+
56+
return idxs;
57+
}
58+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /