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;
}
相关推荐
youngee111 分钟前
hot100-44从前序与中序遍历构造二叉树
数据结构·算法
im_AMBER2 分钟前
Leetcode 68 搜索插入位置 | 寻找比目标字母大的最小字母
数据结构·笔记·学习·算法·leetcode
严文文-Chris4 分钟前
【非监督学习常见算法】
学习·算法·机器学习
CoderYanger6 分钟前
动态规划算法-斐波那契数列模型:1.第N个泰波那契数
开发语言·算法·leetcode·动态规划·1024程序员节
红队it8 分钟前
【机器学习】python旅游数据分析可视化协同过滤算法推荐系统(完整系统源码+数据库+开发笔记+详细部署教程)✅
python·mysql·算法·机器学习·数据分析·旅游
我太想进步了C~~8 分钟前
Prompt Design(提示词工程)入门级了解
前端·人工智能·算法
zore_c10 分钟前
【C语言】文件操作详解2(文件的顺序读写操作)
android·c语言·开发语言·数据结构·笔记·算法·缓存
狐5710 分钟前
2025-12-03-LeetCode刷题笔记-3625-统计梯形的数目-II
笔记·leetcode
大袁同学11 分钟前
【C++完结篇】:深入“次要”但关键的知识腹地
开发语言·数据结构·c++·算法
少许极端12 分钟前
算法奇妙屋(十六)-BFS解决边权为1的多源最短路径问题
算法·bfs·队列·图解算法·边权为1的多源最短路径问题·宽度优先遍历