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;
}
相关推荐
大熊猫侯佩17 分钟前
Swift 数学计算:用 Accelerate 框架让性能“加速吃鸡”
算法·swift
杰克尼38 分钟前
2. 两数相加
算法
无聊的小坏坏39 分钟前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
_Coin_-1 小时前
算法训练营DAY29 第八章 贪心算法 part02
算法·贪心算法
阿维同学1 小时前
自动驾驶关键算法深度研究
人工智能·算法·自动驾驶
今天背单词了吗9802 小时前
算法学习笔记:11.冒泡排序——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·学习·算法·排序算法·冒泡排序
看到我,请让我去学习3 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
倔强的小石头_5 小时前
【C语言指南】函数指针深度解析
java·c语言·算法
Yasin Chen5 小时前
C# Dictionary源码分析
算法·unity·哈希算法
_Coin_-6 小时前
算法训练营DAY27 第八章 贪心算法 part01
算法·贪心算法