1、前言
![](https://img.haomeiwen.com/i11345146/5fe919237435c36a.png)
2、思路
双指针
3、代码
class Solution {
public int[] twoSum(int[] nums, int target) {
if(nums == null || nums.length <= 1){
return new int[]{};
}
int left = 0, right = nums.length - 1;
while(left < right){
int sum = nums[left] + nums[right];
if(sum == target){
return new int[]{nums[left], nums[right]};
}else if(sum < target){
left++;
}else {
right--;
}
}
return new int[]{};
}
}
网友评论