python in 4 lines


  • 0
    Z
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i, v in enumerate(nums):
            if target - v in nums[i+1:]:
                j = nums[i+1:].index(target - v)
                return [i, i+j+1]

  • 0

    Better use index's optional start parameter.


Log in to reply
 

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