天天看点

LeetCode_Array_167. Two Sum II - Input array is sorted两数之和 II - 输入有序数组(C++/Java)1,题目描述2,解题思路3,AC代码4,解题过程

目录

1,题目描述

英文描述

中文描述

2,解题思路

3,AC代码

C++

Java

4,解题过程

第一博

第二搏

1,题目描述

英文描述

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 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.

Example 2:

Input: numbers = [2,3,4], target = 6

Output: [1,3]

Example 3:

Input: numbers = [-1,0], target = -1

Output: [1,2]

中文描述

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。

说明:

返回的下标值(index1 和 index2)不是从零开始的。

你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

示例:

输入: numbers = [2, 7, 11, 15], target = 9

输出: [1,2]

解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2,解题思路

参考@力扣官方题解【两数之和 II - 输入有序数组】。

双指针法,时间复杂度可达O(N);

左右指针分别初始为数组两端;

  • 如果左指针先到达下标 i 的位置,此时右指针还在下标 j 的右侧,sum>target,因此一定是右指针左移,左指针不可能移到 i 的右侧。
  • 同理,右指针也不会跨过最终边界 j;

3,AC代码

C++

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int left = 0, right = numbers.size() - 1, sum;
        while(left < right) {
            sum = numbers[left] + numbers[right];
            if(sum < target) left++;
            else if(sum > target) right--;
            else return{left + 1, right + 1};
        }
        return {-1, -1};
    }
};
           

Java

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int left = 0, right = numbers.length - 1, sum;
        while(left < right) {
            sum = numbers[left] + numbers[right];
            if(sum < target) left++;
            else if(sum > target) right--;
            else return new int[] {left + 1, right + 1};
        }
        return new int[] {-1, -1};
    }
}
           

4,解题过程

第一博

采用双指针的方法,左右指针都从最左边(left = 0,right = 1)开始向右迭代:(由于是左右指针从最左边开始,且只要sum<target,就将左右指针同时右移,所以,右指针的位置不会超过答案的右边界位置,因此sum > target时,只需要调整左指针即可)

  • 指针所指元素之和小于target,左右指针同时右移;
  • 指针所指元素之和大于target,左指针左移;

使用C++测试的效果不太稳定,猜测原因可能是测试使用的数据集不同

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int left = 0, right = 1, sum;
        do {
            sum = numbers[left] + numbers[right];
            if(sum < target){
                left++;
                right++;
            }else if(sum > target) left--;
            else break;
        }while(sum != target);
        return {left + 1, right + 1};
    }
};
           
LeetCode_Array_167. Two Sum II - Input array is sorted两数之和 II - 输入有序数组(C++/Java)1,题目描述2,解题思路3,AC代码4,解题过程

第二搏

查看了官方题解,最优解法都是采用双指针,可以达到O(N)。其中有几点需要注意:
  • 1,双指针具体的使用策略不同。官网给出的是限定两指针分别为数组的左右边界,然后左、右指针分别向中间移动。如果左指针先到达下标 i 的位置,此时右指针还在下标 j 的右侧,sum>target,因此一定是右指针左移,左指针不可能移到 i 的右侧。
  • 2,还需要考虑到无解的情况。第一博中只有 sum == target 时,才会跳出循环。且无解时输出{-1, -1};
LeetCode_Array_167. Two Sum II - Input array is sorted两数之和 II - 输入有序数组(C++/Java)1,题目描述2,解题思路3,AC代码4,解题过程

官网解答中,由于指针分别从左右两端向中间移动,且不会交错,所以最差的情况是遍历数组一遍。

而第一博中最差需要遍历两遍数组。比如[1,2,3,4,5,6,7,8,9,101],target=102,左右指针会一直加到9和10的位置,然后因为sum > target,所以左指针会向后移动,直至到达1的位置。

因此总体的效率较慢。