python solution with one pointer


  • 0
    R

    use one pointer p starts from list end and travels backwards. 32 ms run time
    '''
    class Solution(object):
    def twoSum(self, numbers, target):
    p=len(numbers)-1
    for i, n in enumerate(numbers):
    while p>i and numbers[p]>target-n:
    p-=1
    if numbers[p2]==target-n:
    return [i+1,p+1]
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.