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

这篇具有很好参考价值的文章主要介绍了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<=3104
  • -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
文章来源地址https://www.toymoban.com/news/detail-612646.html


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:
/**
 * 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;
}

到了这里,关于LeetCode //167. Two Sum II - Input Array Is Sorted的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 1. Two Sum 两数之和

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2

    2023年04月25日
    浏览(30)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(37)
  • leetcode167 两数之和 II - 输入有序数组

    定义两个指针分别 l , r l,r l , r 指向数组的最小和最大元素,即左右边界,其中 l l l 向右遍历, r r r 向左遍历 当 l , r l,r l , r 指向的两数之和等于target,就是我们要的结果。如果大于target, r − − r-- r − − ,减小一点。如果小于target, l + + l++ l + + ,增大一点 给你一个下

    2024年02月19日
    浏览(27)
  • 【LeetCode: 167. 两数之和 II - 输入有序数组 | 双指针专题 】

    🚀 算法题 🚀 🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀 🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨ 🌲 作者简介:硕风和炜,CSDN-Java领域优质创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文

    2024年02月13日
    浏览(30)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(36)
  • LeetCode //C - 153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n

    2024年02月06日
    浏览(27)
  • [LeetCode - Python]167.两数之和 II (Medium);125. 验证回文串(Easy)

    167.两数之和 II (Medium) 125. 验证回文串(Easy) 1.自己第一次写: 2.看题解

    2024年02月14日
    浏览(32)
  • Leetcode每日一题:167. 两数之和 II - 输入有序数组(2023.7.8 C++)

    目录 167. 两数之和 II - 输入有序数组 题目描述: 实现代码与解析: 暴力(超时) 双指针 原理思路: 二分 原理思路:         给你一个下标从  1  开始的整数数组  numbers  ,该数组已按   非递减顺序排列   ,请你从数组中找出满足相加之和等于目标数  target  的两

    2024年02月13日
    浏览(38)
  • LeetCode454. 4Sum II

    Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 = i, j, k, l n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 Example 1: Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: (0, 0, 0, 1) - nums1[0] + nums2[0] + nums3[0] +

    2024年02月06日
    浏览(29)
  • Leetcode 82. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Medium Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of n

    2024年02月10日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包