We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2c802d6 commit fae9bc5Copy full SHA for fae9bc5
two_sum.cpp
@@ -0,0 +1,46 @@
1
+class Solution {
2
+public:
3
+ vector<int> twoSum(vector<int>& nums, int target) {
4
+ vector<int> temp = nums;
5
+ sort(temp.begin(), temp.end());
6
+ int val1, val2;
7
+ int a = 0;
8
+ int b = temp.size() - 1;
9
+ while (a < b)
10
+ {
11
+ int sum = temp[a] + temp[b];
12
+ if ( sum > target)
13
+ --b;
14
+ else if ( sum < target )
15
+ ++a;
16
+ else
17
18
+ val1 = temp[a];
19
+ val2 = temp[b];
20
+ break;
21
+ }
22
23
+
24
+ bool find_first = false;
25
+ for(int i = 0; i < nums.size(); ++i)
26
27
+ if (nums[i] == val1 || nums[i] == val2)
28
29
+ if (!find_first)
30
31
+ a = i;
32
+ find_first = true;
33
34
35
36
+ b = i;
37
38
39
40
41
+ temp.clear();
42
+ temp.push_back(a + 1);
43
+ temp.push_back(b + 1);
44
+ return temp;
45
46
+};
0 commit comments