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


//双指针法
class Solution {
    public int[] twoSum(int[] nums, int target) {
        //结果集
        int[] tmp = new int[2];
        
        //双指针,一头一尾
        int start = 0;
        int end = nums.length-1;

        while(start < end){
            //当前俩个指针指向的数字和
            long  curSum = nums[start] + nums[end];

            if(curSum == target){
                tmp[0] = nums[start];
                tmp[1] = nums[end];
                break;
            }
            
            if(curSum < target){
                start++;
            }else{
                end--;
            }
        }
        return tmp;
    }
}