Files
leetcode/167.two-sum-ii-input-array-is-sorted.java
2025-10-28 15:52:13 +08:00

25 lines
541 B
Java

/*
* @lc app=leetcode id=167 lang=java
*
* [167] Two Sum II - Input Array Is Sorted
*/
// @lc code=start
class Solution {
public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length-1;
while(i<j) {
if(numbers[i] + numbers[j] == target) {
return new int[] {i+1, j+1};
} else if(numbers[i] + numbers[j] < target) {
i++;
} else {
j--;
}
}
return new int[]{0, 0};
}
}
// @lc code=end