leetcode Two Sum ----python3

来源:互联网 发布:mac装win10后启动黑屏 编辑:程序博客网 时间:2024/06/07 22:52

看完网上的想法自己写出来的,也算抄了一下把

class Solution:    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        key_list = []        value_list =[]        lens = len(nums)        for i in range(lens):            key_list.append(i)            value_list.append(nums[i])                        n = target - nums[i]            try:                j = value_list.index(n)                if j is not None and j<i:                    return[i,j]            except ValueError as err:                pass


原创粉丝点击