【LeetCode-1】Two Sum

来源:互联网 发布:3d算法必中直选 编辑:程序博客网 时间:2024/05/19 22:59

第一次拿Python写,还不是很熟练,有点迷

class Solution(object):    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        dic = {};            for i in range(len(nums)):            #这里这个用法挺精髓的,运用字典的方法进行查找下标            if target - nums[i] in dic:                     return [dic[target - nums[i]] + 1,i + 1]                        dic[nums[i]] = i


0 0
原创粉丝点击