Best python solution, beats 100 % of python submissions !


  • 2
    S
    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            sorted_nums = sorted(nums)
            (low, high) = (0, len(nums) - 1)
            while low < high:
                if sorted_nums[low] + sorted_nums[high] == target:
                    low_index = nums.index(sorted_nums[low])
                    try:
                        return [low_index, nums[low_index + 1:].index(sorted_nums[high]) + low_index + 1]
                    except:
                        return [nums[:low_index].index(sorted_nums[high]), low_index]
                elif sorted_nums[low] + sorted_nums[high] < target:
                    low += 1
                else:
                    high -= 1
    

Log in to reply
 

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