美文网首页
剑指 Offer 第57题:和为s的两个数字

剑指 Offer 第57题:和为s的两个数字

作者: 放开那个BUG | 来源:发表于2022-08-09 15:00 被阅读0次

1、前言

题目描述

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[]{};
    }
}

相关文章

网友评论

      本文标题:剑指 Offer 第57题:和为s的两个数字

      本文链接:https://www.haomeiwen.com/subject/ztobbrtx.html