Skip to content

Commit a86c0a0

Browse files
authored
Update 0167-Two-Sum-II-Input-array-is-sorted.md
1 parent 129f9a8 commit a86c0a0

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,27 @@
4848
class Solution {
4949
public:
5050
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++;
5667
}
57-
else if(numbers[l] + numbers[r] < target)
58-
l ++;
59-
else // numbers[l] + numbers[r] > target
60-
r --;
6168
}
69+
return {-1, -1};
6270
}
63-
64-
71+
};
6572
```
6673
#### Java
6774
```java

0 commit comments

Comments
 (0)