力扣_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
相关推荐
穿条秋裤到处跑8 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
Merlos_wind8 小时前
HashMap详解
算法·哈希算法·散列表
水蓝烟雨14 小时前
1931. 用三种不同颜色为网格涂色
算法·leetcode
晨曦夜月14 小时前
map与unordered_map区别
算法·哈希算法
我星期八休息15 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表
leoufung15 小时前
LeetCode 76:Minimum Window Substring 题解与滑动窗口思维详解
算法·leetcode·职场和发展
风筝在晴天搁浅17 小时前
LeetCode 92.反转链表Ⅱ
算法·leetcode·链表
普贤莲花21 小时前
【2026年第18周---写于20260501】---舍得
程序人生·算法·leetcode
m0_6294947321 小时前
LeetCode 热题 100-----16.除了自身以外数组的乘积
数据结构·算法·leetcode
We་ct1 天前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·leetcode·typescript·动态规划