Two Sum II - Input array is sorted

Two Sum II - Input array is sorted

Description

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
1. Your returned answers (both index1 and index2) are not zero-based.
2. You may assume that each input would have exactly one solution and you may not use the same element twice.

Example 1:

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.

Tags: Array,HashTable

解读题意

给定一个数组,数组中某两个数加起来等于指定数,求这两个数的下标(从1开始)。

思路1

假设nums为该数组,target为指定数

  1. 使用HashMap作为存储。key为当前值,value为索引。此时要先判断target - nums[i] = 9 - 2 = 7是否存在于map中,若不存在,则插入键值key = 2,value = 0;之后i = 1,num[1] = 7,此时9 - 7 = 2,nums[0] = 2已经存在于map中,那么value = 0 = map.get(2) + 1取出值作为第一个返回值,当前i + 1作为第二个返回值。
class Solution { 

     public int[] twoSum(int[] numbers, int target) {

        if (numbers == null || numbers.length < 2)
            return new int[]{};

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {

            int value = target - numbers[i];
            if (!map.containsKey(value)) {
                map.put(numbers[i], i);
            } else {
                return new int[]{map.get(value) + 1, i + 1};
            }

        }

        return new int[]{};
    }

}

分析:

  • Time complexity:O(n)

思路2

  1. 利用两个指针left和right,分别从数组的头和尾向中间移动,若target == numbers[left] + numbers[right]是否为真,若是则直接返回new int[]{left + 1, right + 1};若numbers[left] + numbers[right] < target,则left指针加1,往右移;否则right指针减1,往左移;
  2. 循环直到left和right指针重合
class Solution { 
    public int[] twoSum(int[] numbers, int target) {

        if (numbers == null || numbers.length < 2)
            return new int[]{};

        int left = 0, right = numbers.length - 1;
        while (left < right) {

            int value = numbers[left] + numbers[right];
            if (value == target) {
                return new int[]{left + 1, right + 1};
            } else if (value < target) {
                left++;
            } else {
                right--;
            }
        }

        return new int[]{};
    }
}

分析:

  • Time complexity:O(n)

leetCode汇总:https://blog.csdn.net/qingtian_1993/article/details/80588941

项目源码,欢迎star:https://github.com/mcrwayfun/java-leet-code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值