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 numbersindex1 and numbersindex2 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 <= numbersi <= 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;
}
相关推荐
ZhengEnCi8 小时前
GPT-6 Astra 爆改 Qwen 权重真有那么神吗:权重空间编程 0.016% 参数的真相
算法·架构
罗西的思考8 小时前
【轻量化复刻大模型 / KIMI K3】KDA(Kimi Delta Attention):并行
人工智能·算法·机器学习
蹊黎8 小时前
Linux 下用 C 语言实现进度条
linux·c语言·单片机
夜不会漫长10 小时前
C++:红黑树
数据结构·算法
爱聊灵光的维C10 小时前
从看见世界到重构认知:在视觉与硬件的交汇处,寻找我们这代人的北极星
c语言·物联网·计算机视觉·重构·硬件工程·射频工程
文丑颜不良啊10 小时前
LeetCode1661~1680题目解析
java·leetcode
AI+程序员在路上11 小时前
RV1126B 上支持的模型及全流程应用--从CNN及语音到 LLMVLM 与跨平台选型
linux·c语言·人工智能
AI备案指南-满满11 小时前
算法备案审核不通过,一般是什么原因?
java·人工智能·算法·大模型备案·算法备案·大模型登记
Ramble_Naylor11 小时前
Rust深度解读:Send 与 Sync 的本质与线程安全边界
python·算法·rust
秋天的怀念_David11 小时前
算法一千零一夜 · 第二十三夜 电力民生写成的曲线
java·程序人生·算法