File tree 1 file changed +18
-11
lines changed
0167-Two-Sum-II-Input-array-is-sorted/Article
1 file changed +18
-11
lines changed Original file line number Diff line number Diff line change 48
48
class Solution {
49
49
public:
50
50
vector<int > twoSum(vector<int >& numbers, int target) {
51
- int l = 0, r = numbers.size() - 1;
52
- while(l < r){
53
- if(numbers[ l] + numbers[ r] == target){
54
- int res[ 2] = {l+1, r+1};
55
- return vector<int >(res, res+2);
51
+ int n = numbers.size();
52
+ int left = 0;
53
+ int right = n-1;
54
+ while(left <= right)
55
+ {
56
+ if(numbers[ left] + numbers[ right] == target)
57
+ {
58
+ return {left + 1, right + 1};
59
+ }
60
+ else if (numbers[ left] + numbers[ right] > target)
61
+ {
62
+ right--;
63
+ }
64
+ else
65
+ {
66
+ left++;
56
67
}
57
- else if(numbers[ l] + numbers[ r] < target)
58
- l ++;
59
- else // numbers[ l] + numbers[ r] > target
60
- r --;
61
68
}
69
+ return {-1, -1};
62
70
}
63
-
64
-
71
+ };
65
72
```
66
73
#### Java
67
74
```java
You can’t perform that action at this time.
0 commit comments