【Leetcode 167】Two Sum II - Input array is sorted

问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数。返回这两个数的下标(从1开始),其中第1个下标比第2个下标小。

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

分析:在排序好的数组中进行查找,很容易想到用二分查找的思想。这里相当于是二分查找两个数,可以把最小值和最大值作为起点求和(sum)。

若sum<target,则需要把较小元素也就是low处元素变大,此时不能直接把mid赋值给low,因为假如numbers[mid] + numbers[high] > target,那么可能存在这两个数一个在(low, mid)区域一个在[mid, high]区域的情况,也就是一个小于numbers[mid]的数x满足x + numbers[high] == target成立,此时直接让low向高位进一格即可。

sum>target的情况同样。

解法:

    vector<int> twoSum(vector<int>& numbers, int target) {
        int low = 0;
        int high = numbers.size() - 1;
        while (low < high) {
            int mid = (low + high) >> 1;
            long sum = numbers[low] + numbers[high];
            if (sum == target)
                return vector<int>{low + 1, high + 1};
            else if (sum > target)
                high = (numbers[low] + numbers[mid] > target) ? mid : high - 1;
            else
                low = (numbers[mid] + numbers[high] <= target) ? mid : low + 1;
        }
        return vector<int>();
    }

比较极端的情况是每次不能移动到二分位置,而是只能进1位或退位,也就是达到O(n)。

但是试了下很难举出这种极端情况的例子,突然直观地觉得好像很难到达O(n)的复杂度……好像还是O(logn)……数学不太好,证明等以后去讨论区看看吧

时间: 2024-10-07 05:25:19

【Leetcode 167】Two Sum II - Input array is sorted的相关文章

leetcode算法: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 m

【LeetCode】167. Two Sum II - Input array is sorted

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 a

LeetCode_167. Two Sum II - Input array is sorted

fsaf 167. Two Sum II - Input array is sorted Easy 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 s

Two Sum II - Input array is sorted(leetcode167) - Solution2

Q: 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 index

Two Sum II - Input array is sorted(leetcode167) - Solution1

Q: 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 index

leetcode2 Two Sum II – Input array is sorted

Two Sum II – Input array is sorted [email protected] Question: Similar to Question [1. Two Sum], except that the input array is already sorted in ascending order. 同上题:先排序,然后从开头和结尾同时向中间查找,原理也比较简单.O(nlogn) runtime, O(1) space vector<int> twoSumSored(v

LeetCode 167. 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 m

leetcode[167] Two Sum II - Input array is sorted

给定数组排好序了,然后给一个目标,找到两个数相加等于目标的两个数的下标. 蛮简单感觉,就是左右两边往里,比目标大就右边减,小就左边加.一样就输出. vector<int> twoSum(vector<int> &numbers, int target){ vector<int> ans; int left = 1, right = numbers.size(); while(left < right){ if (numbers[left-1] + numb

Java [Leetcode 167]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 in