leetcode 刷题-剑指offer-57题

题目[1]

难度简单155收藏分享切换为英文接收动态反馈

输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[2,7] 或者 [7,2]

示例 2:

输入:nums = [10,26,30,31,47,60], target = 40
输出:[10,30] 或者 [30,10]

解答

新手上路,才学疏浅,望斧正

public class Solution13_2 {

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

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

posted @ 2022-01-20 11:42  发呆鱼  阅读(19)  评论(0编辑  收藏  举报