力扣_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
相关推荐
郝学胜-神的一滴10 分钟前
Leetcode 969 煎饼排序✨:翻转间的数组排序艺术
数据结构·c++·算法·leetcode·面试
I_LPL8 小时前
hot100贪心专题
数据结构·算法·leetcode·贪心
灰色小旋风12 小时前
力扣13 罗马数字转整数
数据结构·c++·算法·leetcode
阿里嘎多哈基米14 小时前
速通Hot100-Day09——二叉树
算法·leetcode·二叉树·hot100
Frostnova丶14 小时前
LeetCode 48 & 1886.矩阵旋转与判断
算法·leetcode·矩阵
多打代码14 小时前
2026.3.22 回文子串
算法·leetcode·职场和发展
im_AMBER15 小时前
Leetcode 144 位1的个数 | 只出现一次的数字
学习·算法·leetcode
小刘不想改BUG15 小时前
LeetCode 138.随机链表的复制 Java
java·leetcode·链表·hash table
参.商.16 小时前
【Day43】49. 字母异位词分组
leetcode·golang
参.商.16 小时前
【Day45】647. 回文子串 5. 最长回文子串
leetcode·golang