LeetCode—剑指Offer:和为s的两个数字(双指针法)

和为s的两个数字(简单)

2020年9月10日

题目来源:力扣
在这里插入图片描述

解题
一头一尾两指针,往中间靠

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41541562/article/details/108507755