码迷,mamicode.com
首页 > 编程语言 > 详细

每日LeetCode - 1. 两数之和(Python3)

时间:2021-05-04 15:39:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:两数之和   rate   css   lazy   tar   target   base   div   not   

技术图片

 

#时间复杂度O(N*N),空间复杂度O(1)
#暴力法
def twoSum_baoli(nums: List[int], target:int) -> List[int]:
    for i in range(len(nums)-1):
        base = nums[i]
        for j in range(i+1, len(nums)):
            if base + nums[j]==target:
                return [i,j]
 
#时间复杂度O(N),空间复杂度O(1)
#哈希表
def twoSum(nums: List[int], target:int) -> List[int]:
    dict={}
    for index, num in enumerate(nums):
        another_num = target - num
        if another_num in dict:
            return [dict[another_num], index]
        dict[num] = index

 

每日LeetCode - 1. 两数之和(Python3)

标签:两数之和   rate   css   lazy   tar   target   base   div   not   

原文地址:https://www.cnblogs.com/vicky2021/p/14725462.html

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