Given an ordered array sorted in ascending order, find two numbers so that they are combined and equal to the number of targets.
The function should return these two subscript values index1 and Index2, where index1 must be less than Index2.
Description
- The returned subscript values (INDEX1 and INDEX2) are not zero-based.
- You can assume that each input only corresponds to a unique answer, and that you cannot reuse the same elements.
Example:
input: numbers = [2, 7, one, +], target = 9 output: [up] explanation: 2 and 7 are equal to the target number 9. So index1 = 1, Index2 = 2.
classSolution:deftwosum (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]ifGet_sum <Target:start+=1elifGet_sum >Target:end-=1Else: return(start+1,end+1)
"Leetcode Simple" 37th question two sum II-input ordered array