Short python solution using different approach (~10 lines)

  • 0
    def twoSum(self, nums, target):
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            wrapper_nums = [(num, i) for i, num in enumerate(nums)]
            i, j = 0, len(wrapper_nums) -1
            while i < j:
                curr_sum = wrapper_nums[i][0] + wrapper_nums[j][0]
                if curr_sum == target:   
                    return [wrapper_nums[i][1], wrapper_nums[j][1]]
                elif curr_sum < target:  
                    i += 1
                    j -= 1
            return None

Log in to reply

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