📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
123
返回列表 发新帖
楼主: matsuko
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 小白转码Leetcode 错题本

🔗
 楼主| matsuko 2026-11-11 17:42:54 | 只看该作者
全局:
# Create a 2D DP table with m rows and n columns, all filled with 0
# dp[i][j] will store the number of unique paths to reach cell (i, j)
dp = [[0] * n for _ in range(m)]


回复

使用道具 举报

🔗
 楼主| matsuko 2026-11-13 14:19:21 | 只看该作者
全局:
float("-inf"), float("inf")
负无穷大 和 正无穷大
回复

使用道具 举报

🔗
 楼主| matsuko 2026-11-13 14:33:50 | 只看该作者
全局:
if + if → Both conditions are checked independently. 两个if都会跑!
if + elif + else → Check conditions in order, and execute only the first matching branch.

x = 10

if x > 5:
    print("A") 这个成立了就不会去后面了
elif x > 8:
    print("B")
else:
    print("C")

A
回复

使用道具 举报

🔗
 楼主| matsuko 2026-11-13 14:44:55 | 只看该作者
全局:
# Wrong: can overflow in some languages
m = (l + r) // 2
# Correct: prevents overflow
m = l + (r - l) // 2
回复

使用道具 举报

🔗
 楼主| matsuko 2026-11-13 15:34:16 | 只看该作者
全局:
In Python, break means immediately stop the current loop (for or while).

for i in range(5):
    if i == 2:
        continue
    print(i)

0134 会在continue之后直接跳到下个i

vs

for i in range(5):
    if i == 2:
        break
    print(i)

01 break之后就直接跳过剩下的for loop/while loop
回复

使用道具 举报

🔗
 楼主| matsuko 6 天前 | 只看该作者
全局:
怎么把list里的每个数拷贝到另外一个list?[1, 2, 3, 4] > [1, 2, 3, 4]
nums[:] = res

比如 nums 是 [0, 0, 0, 0],res 是 [1, 2, 3, 4],拷完 nums 就变成 [1, 2, 3, 4] 了。

for i in range(len(res)):
    nums[i] = res[i]
跟这个一样的意思
回复

使用道具 举报

🔗
 楼主| matsuko 昨天 14:11 | 只看该作者
全局:
How to Sort a Dictionary by Values
Sort keys by their values (most common in LeetCode)

d = {'a': 3, 'b': 1, 'c': 2}

# Ascending (small → large)
sorted(d, key=d.get)
# ['b', 'c', 'a']

# Descending (large → small)
sorted(d, key=d.get, reverse=True)
# ['a', 'c', 'b']
回复

使用道具 举报

🔗
 楼主| matsuko 昨天 14:14 | 只看该作者
全局:
matsuko 发表于 2026-11-19 22:11:07
How to Sort a Dictionary by Values
Sort keys by their values (most common in Lee
        # Step 2: Sort by frequency (highest to lowest)
        # count.items() gives (num, freq) pairs
        # key=lambda x: x[1] sorts by the freq part of each pair
        # reverse=True means descending order
        sorted_items = sorted(count.items(), key=lambda x: x[1], reverse=True)

        # Step 3: Take the first k pairs, keep only the num part
        return [num for num, _ in sorted_items[:k]]
回复

使用道具 举报

🔗
 楼主| matsuko 4 小时前 | 只看该作者
全局:
intervals常见sorting with lambda

key = lambda parameter: expression

1. 按照每个 interval 的第一个数字,从小到大排序。
intervals.sort(key=lambda x: x[0])  # 按第一个元素升序

intervals = [[5, 8], [1, 4], [3, 6]]
intervals.sort(key=lambda x: x[0])
print(intervals)
# [[1, 4], [3, 6], [5, 8]]

intervals.sort(key=lambda x: x[1])  # 按第二个元素升序
intervals.sort(key=lambda x: x[0], reverse=True)  # 按第一个元素降序

2. 把 requests 按照 pickup time → return time → record ID 的优先级,从小到大排序。
records = sorted(requests, key=lambda r: (r[1], r[2], r[0]))

Python 会按顺序比较:
1. r[1]:先比较 pickup time。
2. r[2]:如果 pickup time 相同,再比较 return time。
3. r[0]:如果前两个都相同,最后比较 record ID。
第三个 key 的作用叫 tie-breaker(打破平局)。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表