leetcode刷题记录two sum

class Solution(object):
    def twoSum(self, nums, target):
        result = []
        for i in range(0,len(nums)):
            oneNum = nums[i]
            twoNum  = target - oneNum
            if twoNum in nums:
                j = nums.index(twoNum)
                if i != j:
                    result.append(i)
                    result.append(j)
                    return result
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

你可能感兴趣的:(leetcode,leetcode)