【leetcode 簡單】第三十七題 兩數之和 II - 輸入有序數組

來源:互聯網
上載者:User

標籤:lis   而且   目標   ted   bsp   start   輸出   numbers   兩數之和   

給定一個已按照升序排列 的有序數組,找到兩個數使得它們相加之和等於目標數。

函數應該返回這兩個下標值index1 和 index2,其中 index1 必須小於 index2

說明:

  • 返回的下標值(index1 和 index2)不是從零開始的。
  • 你可以假設每個輸入只對應唯一的答案,而且你不可以重複使用相同的元素。

樣本:

輸入: numbers = [2, 7, 11, 15], target = 9輸出: [1,2]解釋: 2 與 7 之和等於目標數 9 。因此 index1 = 1, index2 = 2 。
class Solution:    def twoSum(self, numbers, target):        """        :type numbers: List[int]        :type target: int        :rtype: List[int]        """        start=0        end = len(numbers)-1        while (start<end):            get_sum = numbers[start] + numbers[end]            if get_sum < target:                start+=1            elif get_sum > target:                end-=1            else:                return(start+1,end+1)        

 

【leetcode 簡單】第三十七題 兩數之和 II - 輸入有序數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.