Leetcode167 Two Sum II - Input array is sorted

原题

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.


中文题意

输入一个数组,每个元素为整数,且为升序排序的,在数组中找到两个元素,满足和为一个给定值。数组中的每个元素只能出现一次。

返回结果为所选定元素的位置(位置记录从1开始,且位置1小于位置2)。

每个输入的数组一定会有一个解,如果出现多组元素对满足条件,选择位置1最小的那一组做为结果。


分析

1.输入数组一定会有一个解,那么输入数组的长度至少为2。

2.充分利用输入的数组是升序排序的特点,在检索某一个值时,可以通过大小来简化遍历次数。


java实现

方法一

在数组中找两个满足和为定值的元素,一定需要遍历数组中的元素,那么可以先固定一个元素,假设固定下标为i的元素,(0 <= i<= length - 2),为了满足和为target,那么另一个元素为target-numbers[i],即在剩下的元素中,找到大小为target-numbers[i]的元素。

在剩余的元素中寻找指定的元素,可以遍历寻找

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int another = 0;
        int[] res = new int[2];
        for(int i = 0; i< numbers.length - 1;i++){
            another = target - numbers[i];
            for(int j = i+1;j < numbers.length;j++){
                if(numbers[j] < another){
                    continue;
                }else if(numbers[j] == another){
                    res[0] = i+1;
                    res[1] = j+1;
                    return res;
                }else{
                    break;
                }
            }
        }
        return res;
    }
}

方法二

与方法一的原理一样,只是在剩下的元素中寻找指定元素的时候,采用二分法的方式,加快遍历。

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int start = 0;
        int end = numbers.length -1;
        int mid = 0;
        int another = 0;
        int[] res = new int[2];
        
        for(int i = 0 ; i < numbers.length -1;i++){
            another = target- numbers[i];
            
            start = i+1;
            
            while(start <= end){
                mid = (start + end)/2;
                if(numbers[mid] < another){
                    start = mid + 1;//这里注意,要进行加一操作。
                }else if(numbers[mid] > another){
                    end = mid - 1;//注意这里的减一操作
                }else{
                    res[0] = i+1;
                    res[1] = mid +1;
                    return res;
                }
                
            }
            
        }
        
        return res;
    }
}

方法三

可以从首尾两个方向向中间逼近的方式寻找。

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int x =0;
        int y = numbers.length -1;
        int[] res = new int[2];
        
        while(x < y){
            if(numbers[x] + numbers[y] == target){
                res[0] = x+1;
                res[1] = y+1;
                return res;
            }else if(numbers[x] + numbers[y] < target){
                x++;
            }else{
                y--;
            }
        } 
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值