leetcode第321题sweetLeetCode 1. Two Sum给定一个list,和一个target,要求在list中找到两个元素,使其和为该target,输出这两个元素的index。例如Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]。

Code:


class Solution:

    def twoSum(self, nums, target):

        p = list(range(1000))

        n = []

        for i in p: n.append(-i)

        l = p + n

        l.remove(0)

        for i in l:

            if i in nums:

                num1 = nums.index(i)

                nums[num1] = 'ok'

                left = target - i

                if left in nums:

                    num2 = nums.index(left)

                    return [num1, num2]