码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode1

时间:2021-04-06 14:26:04      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:The   turn   def   sel   image   img   ==   ima   leetcode   

技术图片

解法1

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        dit = {}

        for i in range(len(nums)):

            other = target - nums[i]

            if other not in dit.keys():
                dit[nums[i]] = i

            else:
                return dit[other], i

解法2

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        dit = {}

        for i in range(len(nums)):
            dit[nums[i]] = i

        for i in range(len(nums)):
            other = target - nums[i]
            if other in dit.keys() and dit[other] != i:
                return dit[other], i

解法3

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        for i in range(len(nums)-1):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    return i,j

LeetCode1

标签:The   turn   def   sel   image   img   ==   ima   leetcode   

原文地址:https://www.cnblogs.com/gmbjzg/p/14615811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!