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 751d4d7

Browse files
author
"shubham-1806"
committed
added solution to problem 1 two sum
1 parent 7a3115f commit 751d4d7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎Leetcode/Two Sum/two sum.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
map<int,vector<int>> m;
5+
int i=0;
6+
for(auto x : nums ){
7+
m[x].push_back(i);
8+
i++;
9+
}
10+
i=0;
11+
for(auto x : nums){
12+
if(m.find(target-x)!=m.end()){
13+
if((2*x)==target){
14+
if(m[x].size()>1){
15+
vector<int> v;
16+
v.push_back(m[x][0]);
17+
v.push_back(m[x][1]);
18+
return v;
19+
}
20+
}
21+
else{
22+
vector<int> v;
23+
v.push_back(i);
24+
v.push_back(m[target-x][0]);
25+
return v;
26+
}
27+
}
28+
i++;
29+
}
30+
return {};
31+
}
32+
};

0 commit comments

Comments
(0)

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