Yet another python solution with two pointers


  • 0
    Q
    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            sn = sorted((num, i) for i, num in enumerate(nums))
            i, j = 0, len(nums) - 1
            while i < j:
                n, k = sn[i]
                m, l = sn[j]
                if n + m == target:
                    return [k, l]
                if n + m < target:
                    i += 1
                    continue
                j -= 1
    
    

Log in to reply
 

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