We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
0 parents commit 3a24f13Copy full SHA for 3a24f13
Two Sum.cpp
@@ -0,0 +1,25 @@
1
+class Solution {
2
+public:
3
+ vector<int> twoSum(vector<int>& nums, int target) {
4
+ unordered_map<int,int> m;
5
+ vector<int> result;
6
+ int n = nums.size();
7
+
8
+ for(int i = 0; i<n; i++)
9
+ m.insert(make_pair(nums[i], i));
10
11
+ for(int i = 0; i<n; i++) {
12
+ int key = target-nums[i];
13
+ auto it = m.find(key);
14
+ if(it != m.end() && it->second != i) {
15
+ result.push_back(i);
16
+ result.push_back(it->second);
17
+ return result;
18
+ }
19
20
21
22
23
24
+};
25
0 commit comments