63ms solution using Python with Exception


  • 1
    G
    def twoSum(self, nums, target):
    		s = set(nums)
    		for i in xrange(len(nums)-1):
    			if target - nums[i] in s:
    				try:
    					t = nums.index(target-nums[i], i+1)
    					return (i+1, t+1)
    				except Exception, e:
    					continue
    

    The secret to be fast is to use a set because this structure improve the "sth is in sth" check greatly.


Log in to reply
 

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