力扣_day1

两数之和

hash表的时间复杂度为什么是O(1)?

hash表是基于数组+链表的实现的。数组在内存中是一块连续的空间,只要知道查找数据的下标就可快速定位到数据的内存地址,即数组查找数据的时间复杂度为O(1)。

能用一次循环解决问题就用一次循环。

python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        has = {}
        for i in range(len(nums)):
            key = target - nums[i]
            if key in has:
                return i, has[key]
            has[nums[i]] = i
相关推荐
_深海凉_11 分钟前
LeetCode热题100-买卖股票的最佳时机
leetcode
We་ct12 分钟前
LeetCode 50. Pow(x, n):从暴力法到快速幂的优化之路
开发语言·前端·javascript·算法·leetcode·typescript·
身如柳絮随风扬1 小时前
Redis中的哈希槽怎么理解
redis·哈希算法
Q741_1471 小时前
每日一题 力扣 1320. 二指输入的的最小距离 动态规划 C++ 题解
c++·算法·leetcode·动态规划
wfbcg1 小时前
每日算法练习:LeetCode 76. 最小覆盖子串 ✅
算法·leetcode·职场和发展
wangwangmoon_light2 小时前
1.23 LeetCode总结(树)_一般树
算法·leetcode·职场和发展
wfbcg2 小时前
每日算法练习:LeetCode 30. 串联所有单词的子串 ✅
算法·leetcode·职场和发展
田梓燊2 小时前
leetcode 48
算法·leetcode·职场和发展
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 169. 多数元素 | C++ 哈希表基础解法
c++·leetcode·散列表
米粒12 小时前
力扣算法刷题 Day 38 (打家劫舍专题)
算法·leetcode·职场和发展