21 5 月 2020

leetcode第一题解法1

原题https://leetcode.com/problems/two-sum/submissions/

题目内容 寻找列表两个数之和为指定数字的位置

 

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        b=[]
        for i,val in enumerate(nums):
            tmp=nums[i+1:]
            if (target-val) in tmp:
                b.append(i)
                b.append(i+1+tmp.index(target-val))
        return b