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

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

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

版权

评论