1. 两数之和

1. 两数之和
思路:存起来当前的值和下标 值当k 下标当v 然后判断target-v在不在里面字典里就行

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        '''
        思路:
        用元素作为值,键作为索引
        '''
        dic = {}
        for k,v in enumerate(nums):
            if (target-v) not in dic:
                dic[v] = k
            else:
                return [dic[target-v],k]
               
                    
上一篇:Installing Language Tool in TexStudio


下一篇:罗马数转整数-leetcode刷题C#