Skip to content

Commit 55f7ad1

Browse files
authored
feat: update lc problems (doocs#2517)
1 parent e764667 commit 55f7ad1

File tree

32 files changed

+659
-525
lines changed

32 files changed

+659
-525
lines changed

solution/0100-0199/0191.Number of 1 Bits/README.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,39 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
12-
13-
<p>&nbsp;</p>
14-
15-
<p><strong>提示:</strong></p>
16-
17-
<ul>
18-
<li>请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。</li>
19-
<li>在 Java 中,编译器使用<a href="https://baike.baidu.com/item/二进制补码/5295284" target="_blank">二进制补码</a>记法来表示有符号整数。因此,在&nbsp;<strong>示例 3</strong>&nbsp;中,输入表示有符号整数 <code>-3</code>。</li>
20-
</ul>
11+
<p>编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
2112

2213
<p>&nbsp;</p>
2314

2415
<p><strong>示例 1:</strong></p>
2516

2617
<pre>
27-
<strong>输入:</strong>n = 00000000000000000000000000001011
18+
<strong>输入:</strong>n = 11
2819
<strong>输出:</strong>3
29-
<strong>解释:</strong>输入的二进制串 <code><strong>00000000000000000000000000001011</strong>&nbsp;中,共有三位为 '1'。</code>
20+
<strong>解释:</strong>输入的二进制串 <code><strong>1011</strong>&nbsp;中,共有 3 个设置位。</code>
3021
</pre>
3122

3223
<p><strong>示例 2:</strong></p>
3324

3425
<pre>
35-
<strong>输入:</strong>n = 00000000000000000000000010000000
26+
<strong>输入:</strong>n = 128
3627
<strong>输出:</strong>1
37-
<strong>解释:</strong>输入的二进制串 <strong>00000000000000000000000010000000</strong>&nbsp;中,共有一位为 '1'
28+
<strong>解释:</strong>输入的二进制串 <strong>10000000</strong>&nbsp;中,共有 1 个设置位
3829
</pre>
3930

4031
<p><strong>示例 3:</strong></p>
4132

4233
<pre>
43-
<strong>输入:</strong>n = 11111111111111111111111111111101
44-
<strong>输出:</strong>31
45-
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中,共有 31 位为 '1'。</pre>
34+
<strong>输入:</strong>n = 2147483645
35+
<strong>输出:</strong>30
36+
<strong>解释:</strong>输入的二进制串 <strong>11111111111111111111111111111101</strong> 中,共有 30 个设置位。</pre>
4637

4738
<p>&nbsp;</p>
4839

4940
<p><strong>提示:</strong></p>
5041

5142
<ul>
52-
<li>输入必须是长度为 <code>32</code> 的 <strong>二进制串</strong> 。</li>
43+
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
5344
</ul>
5445

5546
<ul>

solution/0100-0199/0191.Number of 1 Bits/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<p><strong>Constraints:</strong></p>
5050

5151
<ul>
52-
<li>The input must be a <strong>binary string</strong> of length <code>32</code>.</li>
52+
<li><code>1 &lt;= n&nbsp;&lt;= 2<sup>31</sup>&nbsp;- 1</code></li>
5353
</ul>
5454

5555
<p>&nbsp;</p>

solution/0200-0299/0231.Power of Two/README.md

+5-19
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
<p>给你一个整数 <code>n</code>,请你判断该整数是否是 2 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
1212

13-
<p>如果存在一个整数 <code>x</code> 使得 <code>n == 2<sup>x</sup></code> ,则认为 <code>n</code> 是 2 的幂次方。</p>
13+
<p>如果存在一个整数 <code>x</code> 使得&nbsp;<code>n == 2<sup>x</sup></code> ,则认为 <code>n</code> 是 2 的幂次方。</p>
1414

15-
<p> </p>
15+
<p>&nbsp;</p>
1616

1717
<p><strong>示例 1:</strong></p>
1818

@@ -37,29 +37,15 @@
3737
<strong>输出:</strong>false
3838
</pre>
3939

40-
<p><strong>示例 4:</strong></p>
41-
42-
<pre>
43-
<strong>输入:</strong>n = 4
44-
<strong>输出:</strong>true
45-
</pre>
46-
47-
<p><strong>示例 5:</strong></p>
48-
49-
<pre>
50-
<strong>输入:</strong>n = 5
51-
<strong>输出:</strong>false
52-
</pre>
53-
54-
<p> </p>
40+
<p>&nbsp;</p>
5541

5642
<p><strong>提示:</strong></p>
5743

5844
<ul>
59-
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
45+
<li><code>-2<sup>31</sup> &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
6046
</ul>
6147

62-
<p> </p>
48+
<p>&nbsp;</p>
6349

6450
<p><strong>进阶:</strong>你能够不使用循环/递归解决此问题吗?</p>
6551

solution/0300-0399/0321.Create Maximum Number/README.md

+18-24
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,37 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给定长度分别为&nbsp;<code>m</code>&nbsp;&nbsp;<code>n</code>&nbsp;的两个数组,其元素由&nbsp;<code>0-9</code>&nbsp;构成,表示两个自然数各位上的数字。现在从这两个数组中选出 <code>k (k &lt;= m + n)</code>&nbsp;个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。</p>
11+
<p>给你两个整数数组 <code>nums1</code><code>nums2</code>,它们的长度分别为 <code>m</code> 和 <code>n</code>。数组 <code>nums1</code> 和 <code>nums2</code> 分别代表两个数各位上的数字。同时你也会得到一个整数 <code>k</code>。</p>
1212

13-
<p>求满足该条件的最大数。结果返回一个表示该最大数的长度为&nbsp;<code>k</code>&nbsp;的数组。</p>
13+
<p>请你利用这两个数组中的数字中创建一个长度为 <code>k &lt;= m + n</code> 的最大数,在这个必须保留来自同一数组的数字的相对顺序。</p>
1414

15-
<p><strong>说明: </strong>请尽可能地优化你算法的时间和空间复杂度。</p>
15+
<p>返回代表答案的长度为 <code>k</code> 的数组。</p>
1616

17-
<p><strong>示例&nbsp;1:</strong></p>
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
1820

1921
<pre>
20-
<strong>输入:</strong>
21-
nums1 = <code>[3, 4, 6, 5]</code>
22-
nums2 = <code>[9, 1, 2, 5, 8, 3]</code>
23-
k = <code>5</code>
24-
<strong>输出:</strong>
25-
<code>[9, 8, 6, 5, 3]</code></pre>
22+
<strong>输入:</strong>nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
23+
<strong>输出:</strong>[9,8,6,5,3]
24+
</pre>
2625

27-
<p><strong>示例 2:</strong></p>
26+
<p><strong class="example">示例 2</strong></p>
2827

2928
<pre>
30-
<strong>输入:</strong>
31-
nums1 = <code>[6, 7]</code>
32-
nums2 = <code>[6, 0, 4]</code>
33-
k = <code>5</code>
34-
<strong>输出:</strong>
35-
<code>[6, 7, 6, 0, 4]</code></pre>
29+
<strong>输入:</strong>nums1 = [6,7], nums2 = [6,0,4], k = 5
30+
<strong>输出:</strong>[6,7,6,0,4]
31+
</pre>
3632

37-
<p><strong>示例 3:</strong></p>
33+
<p><strong class="example">示例 3</strong></p>
3834

3935
<pre>
40-
<strong>输入:</strong>
41-
nums1 = <code>[3, 9]</code>
42-
nums2 = <code>[8, 9]</code>
43-
k = <code>3</code>
44-
<strong>输出:</strong>
45-
<code>[9, 8, 9]</code>
36+
<strong>输入:</strong>nums1 = [3,9], nums2 = [8,9], k = 3
37+
<strong>输出:</strong>[9,8,9]
4638
</pre>
4739

40+
<p>&nbsp;</p>
41+
4842
<p><strong>提示:</strong></p>
4943

5044
<ul>

solution/0300-0399/0366.Find Leaves of Binary Tree/README.md

+22-34
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,40 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给你一棵二叉树,请按以下要求的顺序收集它的全部节点:</p>
11+
<p>给你一棵二叉树的 <code>root</code> 节点,请按照以下方式收集树的节点:</p>
1212

13-
<ol>
14-
<li>依次从左到右,每次收集并删除所有的叶子节点</li>
15-
<li>重复如上过程直到整棵树为空</li>
16-
</ol>
13+
<ul>
14+
<li>收集所有的叶子节点。</li>
15+
<li>移除所有的叶子节点。</li>
16+
<li>重复以上步骤,直到树为空。</li>
17+
</ul>
1718

1819
<p>&nbsp;</p>
1920

20-
<p><strong>示例:</strong></p>
21-
22-
<pre><strong>输入: </strong>[1,2,3,4,5]
23-
&nbsp;
24-
&nbsp; 1
25-
/ \
26-
2 3
27-
/ \
28-
4 5
29-
30-
<strong>输出: </strong>[[4,5,3],[2],[1]]
21+
<p><strong class="example">示例 1:</strong></p>
22+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0366.Find%20Leaves%20of%20Binary%20Tree/images/remleaves-tree.jpg" style="width: 500px; height: 215px;" />
23+
<pre>
24+
<strong>输入:</strong>root = [1,2,3,4,5]
25+
<strong>输出:</strong>[[4,5,3],[2],[1]]
26+
<strong>解释:</strong>
27+
[[3,5,4],[2],[1]] 和 [[3,4,5],[2],[1]] 也被视作正确答案,因为每一层返回元素的顺序不影响结果。
3128
</pre>
3229

33-
<p>&nbsp;</p>
34-
35-
<p><strong>解释:</strong></p>
36-
37-
<p>1. 删除叶子节点&nbsp;<code>[4,5,3]</code> ,得到如下树结构:</p>
30+
<p><strong class="example">示例 2:</strong></p>
3831

39-
<pre> 1
40-
/
41-
2
32+
<pre>
33+
<strong>输入:</strong>root = [1]
34+
<strong>输出:</strong>[[1]]
4235
</pre>
4336

4437
<p>&nbsp;</p>
4538

46-
<p>2. 现在删去叶子节点&nbsp;<code>[2]</code>&nbsp;,得到如下树结构:</p>
39+
<p><strong>提示:</strong></p>
4740

48-
<pre> 1
49-
</pre>
50-
51-
<p>&nbsp;</p>
52-
53-
<p>3. 现在删去叶子节点&nbsp;<code>[1]</code>&nbsp;,得到空树:</p>
54-
55-
<pre> []
56-
</pre>
41+
<ul>
42+
<li>树中节点的数量在<code>[1, 100]</code>范围内。</li>
43+
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
44+
</ul>
5745

5846
## 解法
5947

solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<p>Given an integer array <code>nums</code> of length <code>n</code> where all the integers of <code>nums</code> are in the range <code>[1, n]</code> and each integer appears <strong>once</strong> or <strong>twice</strong>, return <em>an array of all the integers that appears <strong>twice</strong></em>.</p>
1010

11-
<p>You must write an algorithm that runs in&nbsp;<code>O(n)&nbsp;</code>time and uses only constant extra space.</p>
11+
<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses only constant extra space.</p>
1212

1313
<p>&nbsp;</p>
1414
<p><strong class="example">Example 1:</strong></p>

solution/0700-0799/0760.Find Anagram Mappings/README.md

+22-19
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,40 @@
88

99
<!-- 这里写题目描述 -->
1010

11-
<p>给定两个列表 <code>A</code>and <code>B</code>,并且 <code>B</code> 是 <code>A</code> 的变位(即&nbsp;<code>B</code> 是由&nbsp;<code>A</code> 中的元素随机排列后组成的新列表)。</p>
11+
<p>给你两个整数数组 <code>nums1</code><code>nums2</code>,其中 <code>nums2</code> 是 <code>nums1</code> 的一个<strong> 变位词 </strong>。两个数组都可能包含重复元素。</p>
1212

13-
<p>我们希望找出一个从 <code>A</code><code>B</code>&nbsp;的索引映射 <code>P</code> 。一个映射 <code>P[i] = j</code>&nbsp;指的是列表&nbsp;<code>A</code> 中的第 <code>i</code> 个元素出现于列表&nbsp;<code>B</code> 中的第 <code>j</code> 个元素上。</p>
13+
<p>返回一个下标映射数组 <code>mapping</code>,它将 <code>nums1</code> 映射到 <code>nums2</code>,其中 <code>mapping[i] = j</code> 表示 <code>nums1</code> 中的第 <code>i</code> 个元素出现在 <code>nums2</code> 的第 <code>j</code> 个下标上。如果有多个答案,返回 <strong>任意一个 </strong>。</p>
1414

15-
<p>列表 <code>A</code> 和 <code>B</code> 可能出现重复元素。如果有多于一种答案,输出任意一种。</p>
16-
17-
<p>例如,给定</p>
18-
19-
<pre>A = [12, 28, 46, 32, 50]
20-
B = [50, 12, 32, 46, 28]
21-
</pre>
15+
<p>数组 <code>a</code> 是数组 <code>b</code> 的一个 <strong>变位词 </strong>意味着 <code>b</code> 是通过将 <code>a</code> 中元素的顺序随机打乱生成的。</p>
2216

2317
<p>&nbsp;</p>
2418

25-
<p>需要返回</p>
19+
<p><strong class="example">示例 1:</strong></p>
2620

27-
<pre>[1, 4, 3, 2, 0]
21+
<pre>
22+
<strong>输入:</strong>nums1 = [12,28,46,32,50], nums2 = [50,12,32,46,28]
23+
<strong>输出:</strong>[1,4,3,2,0]
24+
<strong>解释:</strong>因为 nums1 中的第 0 个元素出现在 nums2[1] 上,所以 mapping[0] = 1,而 nums1 中的第 1 个元素出现在 nums2[4] 上,所以 mapping[1] = 4,以此类推。
2825
</pre>
2926

30-
<p><code>P[0] = 1</code>&nbsp;,因为 <code>A</code> 中的第 <code>0</code> 个元素出现于 <code>B[1]</code>,而且 <code>P[1] = 4</code> 因为 <code>A</code> 中第 <code>1</code> 个元素出现于 <code>B[4]</code>,以此类推。</p>
27+
<p><strong class="example">示例 2:</strong></p>
3128

32-
<p>&nbsp;</p>
29+
<pre>
30+
<strong>输入:</strong>nums1 = [84,46], nums2 = [84,46]
31+
<strong>输出:</strong>[0,1]
32+
</pre>
3333

34-
<p><strong>注:</strong></p>
34+
<p>&nbsp;</p>
3535

36-
<ol>
37-
<li><code>A, B</code>&nbsp;有相同的长度,范围为&nbsp;<code>[1, 100]</code>。</li>
38-
<li><code>A[i], B[i]</code> 都是范围在&nbsp;<code>[0, 10^5]</code> 的整数。</li>
39-
</ol>
36+
<p><strong>提示:</strong></p>
4037

41-
<p>&nbsp;</p>
38+
<ul>
39+
<li><code>1 &lt;= nums1.length &lt;= 100</code></li>
40+
<li><code>nums2.length == nums1.length</code></li>
41+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li>
42+
<li><code>nums2</code> 是 <code>nums1</code> 的一个变位词。</li>
43+
</ul>
44+
<!-- 保持注释以帮助理解题目要求 -->
4245

4346
## 解法
4447

solution/0800-0899/0866.Prime Palindrome/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0800-0899/0866.Prime%20Palindrome/README_EN.md)
44

5-
<!-- tags:数学 -->
5+
<!-- tags:数学,数论 -->
66

77
## 题目描述
88

solution/0800-0899/0866.Prime Palindrome/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0800-0899/0866.Prime%20Palindrome/README.md)
44

5-
<!-- tags:Math -->
5+
<!-- tags:Math,Number Theory -->
66

77
## Description
88

solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is clo
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Hash Table + Priority Queue (Max Heap)
59+
60+
According to the problem description, we need to design a data structure that supports popping out the element with the highest frequency. If multiple elements have the same frequency, the element closest to the top of the stack should be popped out.
61+
62+
We can use a hash table $cnt$ to record the frequency of each element, and a priority queue (max heap) $q$ to maintain the frequency of elements and their corresponding timestamps.
63+
64+
When performing a push operation, we first increment the current timestamp, i.e., $ts \gets ts + 1$; then we increment the frequency of the element $val$, i.e., $cnt[val] \gets cnt[val] + 1$, and finally, we add the triplet $(cnt[val], ts, val)$ to the priority queue $q$. The time complexity of the push operation is $O(\log n)$.
65+
66+
When performing a pop operation, we directly pop an element from the priority queue $q$. Since the elements in the priority queue $q$ are sorted in descending order of frequency, the popped element is definitely the one with the highest frequency. If multiple elements have the same frequency, the element closest to the top of the stack is popped out, i.e., the element with the largest timestamp is popped out. After popping, we decrement the frequency of the popped element, i.e., $cnt[val] \gets cnt[val] - 1$. The time complexity of the pop operation is $O(\log n)$.
5967

6068
<!-- tabs:start -->
6169

@@ -189,7 +197,15 @@ func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; retur
189197

190198
<!-- tabs:end -->
191199

192-
### Solution 2
200+
### Solution 2: Double Hash Tables
201+
202+
In Solution 1, in order to pop out the required element, we maintained a priority queue and had to operate on it each time, which has a time complexity of $O(\log n)$. If we can find the required element in $O(1)$ time, then the time complexity of each operation of the entire data structure can be reduced to $O(1)$.
203+
204+
In fact, we can use a variable $mx$ to record the current maximum frequency, a hash table $d$ to record the list of elements corresponding to each frequency, and as in Solution 1, a hash table $cnt$ to record the frequency of each element.
205+
206+
When performing a push operation, we increment the frequency of the element, i.e., $cnt[val] \gets cnt[val] + 1$, and then add the element $val$ to the corresponding frequency list in the hash table $d$, i.e., $d[cnt[val]].push(val)$. If the current element's frequency is greater than $mx$, then update $mx$, i.e., $mx \gets cnt[val]$. The time complexity of the push operation is $O(1)$.
207+
208+
When performing a pop operation, we take the list of elements with frequency $mx$ from the hash table $d$, pop out the last element $val$ in the list, and then remove $val$ from the hash table $d$, i.e., $d[mx].pop()$. Finally, we decrement the frequency of $val$, i.e., $cnt[val] \gets cnt[val] - 1$. If the list $d[mx]$ is empty, it means that all elements with the current maximum frequency have been popped out, and we need to decrement $mx$, i.e., $mx \gets mx - 1$. The time complexity of the pop operation is $O(1)$.
193209

194210
<!-- tabs:start -->
195211

0 commit comments

Comments
 (0)