国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode //167. Two Sum II - Input Array Is Sorted。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

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
文章來源地址http://www.zghlxwxcb.cn/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;
}

到了這里,關(guān)于LeetCode //167. Two Sum II - Input Array Is Sorted的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • LeetCode 1. Two Sum 兩數(shù)之和

    題目描述 給定一個(gè)整數(shù)數(shù)組 nums?和一個(gè)目標(biāo)值 target,請你在該數(shù)組中找出和為目標(biāo)值的那?兩個(gè)?整數(shù),并返回他們的數(shù)組下標(biāo)。 你可以假設(shè)每種輸入只會(huì)對應(yīng)一個(gè)答案。但是,數(shù)組中同一個(gè)元素不能使用兩遍。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因?yàn)?nums[0] + nums[1] = 2

    2023年04月25日
    瀏覽(24)
  • 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日
    瀏覽(19)
  • leetcode167 兩數(shù)之和 II - 輸入有序數(shù)組

    定義兩個(gè)指針分別 l , r l,r l , r 指向數(shù)組的最小和最大元素,即左右邊界,其中 l l l 向右遍歷, r r r 向左遍歷 當(dāng) l , r l,r l , r 指向的兩數(shù)之和等于target,就是我們要的結(jié)果。如果大于target, r ? ? r-- r ? ? ,減小一點(diǎn)。如果小于target, l + + l++ l + + ,增大一點(diǎn) 給你一個(gè)下

    2024年02月19日
    瀏覽(30)
  • 【LeetCode: 167. 兩數(shù)之和 II - 輸入有序數(shù)組 | 雙指針專題 】

    【LeetCode: 167. 兩數(shù)之和 II - 輸入有序數(shù)組 | 雙指針專題 】

    ?? 算法題 ?? ?? 算法刷題專欄 | 面試必備算法 | 面試高頻算法 ?? ?? 越難的東西,越要努力堅(jiān)持,因?yàn)樗哂泻芨叩膬r(jià)值,算法就是這樣? ?? 作者簡介:碩風(fēng)和煒,CSDN-Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者??,保研|國家獎(jiǎng)學(xué)金|高中學(xué)習(xí)JAVA|大學(xué)完善JAVA開發(fā)技術(shù)棧|面試刷題|面經(jīng)八股文

    2024年02月13日
    瀏覽(53)
  • 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日
    瀏覽(31)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    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日
    瀏覽(18)
  • [LeetCode - Python]167.兩數(shù)之和 II (Medium);125. 驗(yàn)證回文串(Easy)

    [LeetCode - Python]167.兩數(shù)之和 II (Medium);125. 驗(yàn)證回文串(Easy)

    167.兩數(shù)之和 II (Medium) 125. 驗(yàn)證回文串(Easy) 1.自己第一次寫: 2.看題解

    2024年02月14日
    瀏覽(20)
  • Leetcode每日一題:167. 兩數(shù)之和 II - 輸入有序數(shù)組(2023.7.8 C++)

    Leetcode每日一題:167. 兩數(shù)之和 II - 輸入有序數(shù)組(2023.7.8 C++)

    目錄 167.?兩數(shù)之和 II - 輸入有序數(shù)組 題目描述: 實(shí)現(xiàn)代碼與解析: 暴力(超時(shí)) 雙指針 原理思路: 二分 原理思路: ????????給你一個(gè)下標(biāo)從? 1 ?開始的整數(shù)數(shù)組? numbers ?,該數(shù)組已按 ? 非遞減順序排列?? ,請你從數(shù)組中找出滿足相加之和等于目標(biāo)數(shù)? target ?的兩

    2024年02月13日
    瀏覽(29)
  • 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日
    瀏覽(19)
  • 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日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包