剑指 Offer 57. 和为s的两个数字


因为是有序的,所以用双指针就行

https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int i = 0, j = nums.length - 1;
        while(i < j){
            int sum = nums[i] + nums[j];
            if(sum > target) j--;
            else if(sum < target) i++;
            else return new int[] {nums[i], nums[j]};
        }
        return new int[0];
    }
}