LeetCode //167. Two Sum II - Input Array Is Sorted

167. Two Sum II - Input Array Is Sorted

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 < numbers.length.

Return the indices of the two numbers, index1 and index2 , added by one as an integer array [index1, index2] of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

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. We return [1, 2].

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].

Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].

Constraints:

  • 2 < = n u m b e r s . l e n g t h < = 3 ∗ 1 0 4 2 <= numbers.length <= 3 * 10^4 2<=numbers.length<=3∗104
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order.
  • -1000 <= target <= 1000
  • The tests are generated such that there is exactly one solution.

From: LeetCode

Link: 167. Two Sum II - Input Array Is Sorted


Solution:

Ideas:
We start with one pointer at the beginning of the array and another at the end. If the sum of the numbers at the two pointers is less than the target, we move the pointer at the beginning forward to increase the sum. If the sum is greater than the target, we move the pointer at the end backward to decrease the sum. We continue this process until we find two numbers that add up to the target.
Code:
c 复制代码
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
    *returnSize = 2;
    int* result = (int*) malloc(*returnSize * sizeof(int));
    int left = 0, right = numbersSize - 1;

    while(left < right){
        int sum = numbers[left] + numbers[right];

        if(sum == target){
            result[0] = left + 1;  // +1 because the problem is 1-indexed
            result[1] = right + 1; // +1 because the problem is 1-indexed
            return result;
        }
        else if(sum < target)
            left++;
        else
            right--;
    }

    // This point should never be reached because the problem guarantees a solution
    return NULL;
}
相关推荐
BigCowPeking几秒前
leetcode 排序算法汇总
算法·leetcode·排序算法
eternal__day5 分钟前
优选算法(双指针)
算法·leetcode·推荐算法
武昌库里写JAVA16 分钟前
SpringBoot+SpringCloud面试题整理附答案
java·开发语言·算法·spring·log4j
手握风云-22 分钟前
数据结构(Java版)第五期:ArrayList与顺序表(下)
java·数据结构·算法
不去幼儿园23 分钟前
【RL Base】多级反馈队列(MFQ)算法
人工智能·python·算法·机器学习·强化学习
是糖不是唐31 分钟前
代码随想录算法训练营第五十四天|Day54 图论
c语言·数据结构·算法·图论
Shiroha Wang43 分钟前
【数据结构OJ】【图论】货币套汇(图路径)
数据结构·c++·经验分享·笔记·算法·图论
我怎么又饿了呀1 小时前
DataWhale—PumpkinBook(TASK05决策树)
算法·决策树·机器学习
DW_DROME1 小时前
02向量与矩阵方程
线性代数·算法·矩阵
Sunyanhui11 小时前
力扣 最大数组和-53
数据结构·算法·leetcode