Python O(N) solution


  • 0
    A
    class Solution(object):
        def twoSum(self, numbers, target):
            """
            :type numbers: List[int]
            :type target: int
            :rtype: List[int]
            """
            if not numbers:
                return []
            ind1 = 0
            ind2 = len(numbers) - 1
            while ind1 < ind2:
                sumv = numbers[ind1] + numbers[ind2]
                if sumv == target:
                    return [ind1+1, ind2+1]
                elif sumv > target:
                    ind2-=1
                else:
                    ind1+=1
            
                    
    

Log in to reply
 

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