feat: 2025-10-28打卡
This commit is contained in:
24
167.two-sum-ii-input-array-is-sorted.java
Normal file
24
167.two-sum-ii-input-array-is-sorted.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @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
|
||||
|
||||
Reference in New Issue
Block a user