File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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
+ else
35
+ {
36
+ b = i;
37
+ break ;
38
+ }
39
+ }
40
+ }
41
+ temp.clear ();
42
+ temp.push_back (a + 1 );
43
+ temp.push_back (b + 1 );
44
+ return temp;
45
+ }
46
+ };
You can’t perform that action at this time.
0 commit comments