【附JS、Python、C++题解】Leetcode面试150题(7)

一、题目

167. 两数之和 II - 输入有序数组

给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列  ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 

numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。

以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1  index2

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

你所设计的解决方案必须只使用常量级的额外空间。

二、思路

1. 回顾一下1. 两数之和,说的是“给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。”区别在哪里?

① 该问题中添加了“非递减顺序排列”这个条件【可以用来简化遍历逻辑】;

② 返回值不同,一个返回新构建的数组,一个返回数组下标【不影响复杂度】;

③ 限制条件不同,该问题中的限制条件有:两个下标的范围&数组的长度限制。

可以看到重点就在①,即对遍历循环这一部分逻辑的处理。

三、代码

① JavaScript:

function TargetIndex(nums, target) {
    if (nums.length < 2) {
        return []; // 或者抛出错误
    }
    let left = 0;
    let right = nums.length - 1;

    while (left < right) {
        const sum = nums[left] + nums[right];
        if (sum === target) {
            return [left + 1, right + 1]; // 返回的下标从 1 开始
        } else if (sum < target) {
            left++;
        } else {
            right--;
        }
    }
    return []; // 如果没有找到符合条件的数对
}

② Python:

def target_index(nums, target):
    if len(nums) < 2:
        return []  

    left = 0
    right = len(nums) - 1

    while left < right:
        total = nums[left] + nums[right]
        if total == target:
            return [left + 1, right + 1]  
        elif total < target:
            left += 1
        else:
            right -= 1

    return []  #返回空数组

③ C++:

vector targetIndex(vector nums, int target){
    int length = nums.size();
    if(length<2){
        return {};
    }
    int left = 0;
    int right = length -1;
    while (left < right) {
        int sum = nums[left] + nums[right];
        if (sum == target) {
            return {left + 1, right + 1};  // 返回的下标从 1 开始
        } else if (sum < target) {
            left++;
        } else {
            right--;
        }
    }
    return {};

}

四、反思

我的代码和官方题解对照如下:(注释掉的是我的代码)

【附JS、Python、C++题解】Leetcode面试150题(7)_第1张图片

对照两段代码,一个是代码的简洁性上(定义变量减少重复),另一个是逻辑的严密性上(几个限定条件) ;下次解决类似问题,要多考虑这两个方面!

你可能感兴趣的:(leetcode整理,javascript,python,c++)