Skip to content

Commit bfaf55b

Browse files
authored
feat: update lc problems (#3419)
1 parent 162cdab commit bfaf55b

File tree

43 files changed

+545
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+545
-179
lines changed

solution/0000-0099/0001.Two Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags:
1919

2020
<p>给定一个整数数组 <code>nums</code>&nbsp;和一个整数目标值 <code>target</code>,请你在该数组中找出 <strong>和为目标值 </strong><em><code>target</code></em>&nbsp; 的那&nbsp;<strong>两个</strong>&nbsp;整数,并返回它们的数组下标。</p>
2121

22-
<p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p>
22+
<p>你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。</p>
2323

2424
<p>你可以按任意顺序返回答案。</p>
2525

solution/0000-0099/0016.3Sum Closest/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ tags:
3131
<pre>
3232
<strong>输入:</strong>nums = [-1,2,1,-4], target = 1
3333
<strong>输出:</strong>2
34-
<strong>解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2)
34+
<strong>解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2)。
3535
</pre>
3636

3737
<p><strong>示例 2:</strong></p>
3838

3939
<pre>
4040
<strong>输入:</strong>nums = [0,0,0], target = 1
4141
<strong>输出:</strong>0
42-
</pre>
42+
<strong>解释:</strong>与 target 最接近的和是 0(0 + 0 + 0 = 0)。</pre>
4343

4444
<p>&nbsp;</p>
4545

solution/0000-0099/0071.Simplify Path/README.md

+57-27
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的 Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p>
20+
<p>给你一个字符串 <code>path</code> ,表示指向某一文件或目录的&nbsp;Unix 风格 <strong>绝对路径 </strong>(以 <code>'/'</code> 开头),请你将其转化为更加简洁的规范路径。</p>
2121

22-
<p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p>
22+
<p class="MachineTrans-lang-zh-CN">在 Unix 风格的文件系统中,一个点(<code>.</code>)表示当前目录本身;此外,两个点 (<code>..</code>)&nbsp;表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,<code>'//'</code>)都被视为单个斜杠 <code>'/'</code> 。 对于此问题,任何其他格式的点(例如,<code>'...'</code>)均被视为文件/目录名称。</p>
2323

2424
<p>请注意,返回的 <strong>规范路径</strong> 必须遵循下述格式:</p>
2525

@@ -32,44 +32,74 @@ tags:
3232

3333
<p>返回简化后得到的 <strong>规范路径</strong> 。</p>
3434

35-
<p> </p>
35+
<p>&nbsp;</p>
3636

37-
<p><strong>示例 1:</strong></p>
37+
<p><strong class="example">示例 1:</strong></p>
3838

39-
<pre>
40-
<strong>输入:</strong>path = "/home/"
41-
<strong>输出:</strong>"/home"
42-
<strong>解释:</strong>注意,最后一个目录名后面没有斜杠。 </pre>
39+
<div class="example-block">
40+
<p><span class="example-io"><b>输入:</b>path = "/home/"</span></p>
4341

44-
<p><strong>示例 2:</strong></p>
42+
<p><span class="example-io"><b>输出:</b>"/home"</span></p>
4543

46-
<pre>
47-
<strong>输入:</strong>path = "/../"
48-
<strong>输出:</strong>"/"
49-
<strong>解释:</strong>从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。
50-
</pre>
44+
<p><strong>解释:</strong></p>
5145

52-
<p><strong>示例 3:</strong></p>
46+
<p>应删除尾部斜杠。</p>
47+
</div>
5348

54-
<pre>
55-
<strong>输入:</strong>path = "/home//foo/"
56-
<strong>输出:</strong>"/home/foo"
57-
<strong>解释:</strong>在规范路径中,多个连续斜杠需要用一个斜杠替换。
58-
</pre>
49+
<p><strong class="example">示例 2:</strong></p>
5950

60-
<p><strong>示例 4:</strong></p>
51+
<div class="example-block">
52+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/home//foo/"</span></p>
6153

62-
<pre>
63-
<strong>输入:</strong>path = "/a/./b/../../c/"
64-
<strong>输出:</strong>"/c"
65-
</pre>
54+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/home/foo"</span></p>
6655

67-
<p> </p>
56+
<p><strong>解释:</strong></p>
57+
58+
<p>多个连续的斜杠被单个斜杠替换。</p>
59+
</div>
60+
61+
<p><strong class="example">示例 3:</strong></p>
62+
63+
<div class="example-block">
64+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/home/user/Documents/../Pictures"</span></p>
65+
66+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/home/user/Pictures"</span></p>
67+
68+
<p><strong>解释:</strong></p>
69+
70+
<p>两个点&nbsp;<code>".."</code>&nbsp;表示上一级目录。</p>
71+
</div>
72+
73+
<p><strong class="example">示例 4:</strong></p>
74+
75+
<div class="example-block">
76+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/../"</span></p>
77+
78+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/"</span></p>
79+
80+
<p><strong>解释:</strong></p>
81+
82+
<p>不可能从根目录上升级一级。</p>
83+
</div>
84+
85+
<p><strong class="example">示例 5:</strong></p>
86+
87+
<div class="example-block">
88+
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/.../a/../b/c/../d/./"</span></p>
89+
90+
<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/.../b/d"</span></p>
91+
92+
<p><strong>解释:</strong></p>
93+
94+
<p><code>"..."</code> 是此问题中目录的有效名称。</p>
95+
</div>
96+
97+
<p>&nbsp;</p>
6898

6999
<p><strong>提示:</strong></p>
70100

71101
<ul>
72-
<li><code>1 <= path.length <= 3000</code></li>
102+
<li><code>1 &lt;= path.length &lt;= 3000</code></li>
73103
<li><code>path</code> 由英文字母,数字,<code>'.'</code>,<code>'/'</code> 或 <code>'_'</code> 组成。</li>
74104
<li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li>
75105
</ul>

solution/0200-0299/0230.Kth Smallest Element in a BST/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tags:
1111

1212
<!-- problem:start -->
1313

14-
# [230. 二叉搜索树中第K小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-bst)
14+
# [230. 二叉搜索树中第 K 小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-bst)
1515

1616
[English Version](/solution/0200-0299/0230.Kth%20Smallest%20Element%20in%20a%20BST/README_EN.md)
1717

solution/0300-0399/0308.Range Sum Query 2D - Mutable/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tags:
1212

1313
<!-- problem:start -->
1414

15-
# [308. 二维区域和检索 - 可变 🔒](https://leetcode.cn/problems/range-sum-query-2d-mutable)
15+
# [308. 二维区域和检索 - 矩阵可修改 🔒](https://leetcode.cn/problems/range-sum-query-2d-mutable)
1616

1717
[English Version](/solution/0300-0399/0308.Range%20Sum%20Query%202D%20-%20Mutable/README_EN.md)
1818

solution/0400-0499/0457.Circular Array Loop/README.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,33 @@ tags:
3939

4040
<p>&nbsp;</p>
4141

42-
<p><strong>示例 1:</strong></p>
43-
42+
<p><strong class="example">示例 1:</strong></p>
43+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0457.Circular%20Array%20Loop/images/1723688159-qYjpWT-image.png" style="width: 402px; height: 289px;" />
4444
<pre>
4545
<strong>输入:</strong>nums = [2,-1,1,2,2]
4646
<strong>输出:</strong>true
47-
<strong>解释:</strong>存在循环,按下标 0 -&gt; 2 -&gt; 3 -&gt; 0 。循环长度为 3 。
47+
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
48+
我们可以看到存在循环,按下标 0 -&gt; 2 -&gt; 3 -&gt; 0 --&gt; ...,并且其中的所有节点都是白色(以相同方向跳跃)。
4849
</pre>
4950

50-
<p><strong>示例 2:</strong></p>
51-
51+
<p><strong class="example">示例 2:</strong></p>
52+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0457.Circular%20Array%20Loop/images/1723688183-lRSkjp-image.png" style="width: 402px; height: 390px;" />
5253
<pre>
53-
<strong>输入:</strong>nums = [-1,2]
54+
<strong>输入:</strong>nums = [-1,-2,-3,-4,-5,6]
5455
<strong>输出:</strong>false
55-
<strong>解释:</strong>按下标 1 -&gt; 1 -&gt; 1 ... 的运动无法构成循环,因为循环的长度为 1 。根据定义,循环的长度必须大于 1 。
56+
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
57+
唯一的循环长度为 1,所以返回 false。
5658
</pre>
5759

58-
<p><strong>示例 3:</strong></p>
59-
60+
<p><strong class="example">示例 3</strong></p>
61+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0457.Circular%20Array%20Loop/images/1723688199-nhaMuF-image.png" style="width: 497px; height: 242px;" />
6062
<pre>
61-
<strong>输入:</strong>nums = [-2,1,-1,-2,-2]
62-
<strong>输出:</strong>false
63-
<strong>解释:</strong>按下标 1 -&gt; 2 -&gt; 1 -&gt; ... 的运动无法构成循环,因为 nums[1] 是正数,而 nums[2] 是负数。
64-
所有 nums[seq[j]] 应当不是全正就是全负。</pre>
63+
<strong>输入:</strong>nums = [1,-1,5,1,4]
64+
<strong>输出:</strong>true
65+
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
66+
我们可以看到存在循环,按下标 0 --&gt; 1 --&gt; 0 --&gt; ...,当它的大小大于 1 时,它有一个向前跳的节点和一个向后跳的节点,所以 <strong>它不是一个循环</strong>。
67+
我们可以看到存在循环,按下标 3 --&gt; 4 --&gt; 3 --&gt; ...,并且其中的所有节点都是白色(以相同方向跳跃)。
68+
</pre>
6569

6670
<p>&nbsp;</p>
6771

Loading
Loading
Loading

solution/0600-0699/0638.Shopping Offers/README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929

3030
<p>返回<strong> 确切 </strong>满足购物清单所需花费的最低价格,你可以充分利用大礼包的优惠活动。你不能购买超出购物清单指定数量的物品,即使那样会降低整体价格。任意大礼包可无限次购买。</p>
3131

32-
<p> </p>
32+
<p>&nbsp;</p>
3333

3434
<p><strong>示例 1:</strong></p>
3535

@@ -51,19 +51,18 @@ tags:
5151
需要买 1A ,2B 和 1C ,所以付 ¥4 买 1A 和 1B(大礼包 1),以及 ¥3 购买 1B , ¥4 购买 1C 。
5252
不可以购买超出待购清单的物品,尽管购买大礼包 2 更加便宜。</pre>
5353

54-
<p> </p>
54+
<p>&nbsp;</p>
5555

5656
<p><strong>提示:</strong></p>
5757

5858
<ul>
59-
<li><code>n == price.length</code></li>
60-
<li><code>n == needs.length</code></li>
61-
<li><code>1 <= n <= 6</code></li>
62-
<li><code>0 <= price[i] <= 10</code></li>
63-
<li><code>0 <= needs[i] <= 10</code></li>
64-
<li><code>1 <= special.length <= 100</code></li>
59+
<li><code>n == price.length == needs.length</code></li>
60+
<li><code>1 &lt;= n &lt;= 6</code></li>
61+
<li><code>0 &lt;= price[i], needs[i] &lt;= 10</code></li>
62+
<li><code>1 &lt;= special.length &lt;= 100</code></li>
6563
<li><code>special[i].length == n + 1</code></li>
66-
<li><code>0 <= special[i][j] <= 50</code></li>
64+
<li><code>0 &lt;= special[i][j] &lt;= 50</code></li>
65+
<li>生成的输入对于&nbsp;<code>0 &lt;= j &lt;= n - 1</code> 至少有一个&nbsp;<code>special[i][j]</code>&nbsp;非零。</li>
6766
</ul>
6867

6968
<!-- description:end -->

solution/0600-0699/0638.Shopping Offers/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You cannot add more items, though only $9 for 2A ,2B and 1C.
6262
<li><code>1 &lt;= special.length &lt;= 100</code></li>
6363
<li><code>special[i].length == n + 1</code></li>
6464
<li><code>0 &lt;= special[i][j] &lt;= 50</code></li>
65+
<li>The input is generated that at least one of <code>special[i][j]</code> is non-zero for <code>0 &lt;= j &lt;= n - 1</code>.</li>
6566
</ul>
6667

6768
<!-- description:end -->

solution/0700-0799/0777.Swap Adjacent in LR String/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>In a string composed of <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;X&#39;</code> characters, like <code>&quot;RXXLRXRXL&quot;</code>, a move consists of either replacing one occurrence of <code>&quot;XL&quot;</code> with <code>&quot;LX&quot;</code>, or replacing one occurrence of <code>&quot;RX&quot;</code> with <code>&quot;XR&quot;</code>. Given the starting string <code>start</code> and the ending string <code>end</code>, return <code>True</code> if and only if there exists a sequence of moves to transform one string to the other.</p>
20+
<p>In a string composed of <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;X&#39;</code> characters, like <code>&quot;RXXLRXRXL&quot;</code>, a move consists of either replacing one occurrence of <code>&quot;XL&quot;</code> with <code>&quot;LX&quot;</code>, or replacing one occurrence of <code>&quot;RX&quot;</code> with <code>&quot;XR&quot;</code>. Given the starting string <code>start</code> and the ending string <code>end</code>, return <code>True</code> if and only if there exists a sequence of moves to transform <code>start</code> to <code>end</code>.</p>
2121

2222
<p>&nbsp;</p>
2323
<p><strong class="example">Example 1:</strong></p>

solution/0700-0799/0787.Cheapest Flights Within K Stops/README.md

+22-18
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,36 @@ tags:
2828
<p>&nbsp;</p>
2929

3030
<p><strong>示例 1:</strong></p>
31-
31+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0787.Cheapest%20Flights%20Within%20K%20Stops/images/cheapest-flights-within-k-stops-3drawio.png" style="width: 332px; height: 392px;" />
3232
<pre>
3333
<strong>输入:</strong>
34-
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
35-
src = 0, dst = 2, k = 1
36-
<strong>输出:</strong> 200
37-
<strong>解释:</strong>
38-
城市航班图如下
39-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0787.Cheapest%20Flights%20Within%20K%20Stops/images/995.png" style="height: 180px; width: 246px;" />
40-
41-
从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。</pre>
34+
n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
35+
<strong>输出:</strong> 700
36+
<strong>解释:</strong> 城市航班图如上
37+
从城市 0 到城市 3 经过最多 1 站的最佳路径用红色标记,费用为 100 + 600 = 700。
38+
请注意,通过城市 [0, 1, 2, 3] 的路径更便宜,但无效,因为它经过了 2 站。
39+
</pre>
4240

4341
<p><strong>示例 2:</strong></p>
44-
42+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0787.Cheapest%20Flights%20Within%20K%20Stops/images/cheapest-flights-within-k-stops-1drawio.png" style="width: 332px; height: 242px;" />
4543
<pre>
4644
<strong>输入:</strong>
47-
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
48-
src = 0, dst = 2, k = 0
49-
<strong>输出:</strong> 500
45+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
46+
<strong>输出:</strong> 200
5047
<strong>解释:</strong>
51-
城市航班图如下
52-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0787.Cheapest%20Flights%20Within%20K%20Stops/images/995.png" style="height: 180px; width: 246px;" />
53-
54-
从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。</pre>
48+
城市航班图如上
49+
从城市 0 到城市 2 经过最多 1 站的最佳路径标记为红色,费用为 100 + 100 = 200。
50+
</pre>
5551

56-
<p>&nbsp;</p>
52+
<p><strong class="example">示例 3:</strong></p>
53+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0787.Cheapest%20Flights%20Within%20K%20Stops/images/cheapest-flights-within-k-stops-2drawio.png" style="width: 332px; height: 242px;" />
54+
<pre>
55+
<b>输入:</b>n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
56+
<b>输出:</b>500
57+
<strong>解释:</strong>
58+
城市航班图如上
59+
从城市 0 到城市 2 不经过站点的最佳路径标记为红色,费用为 500。
60+
</pre>
5761

5862
<p><strong>提示:</strong></p>
5963

Binary file not shown.

solution/0800-0899/0841.Keys and Rooms/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tags:
2020

2121
<p>有 <code>n</code> 个房间,房间按从 <code>0</code> 到 <code>n - 1</code> 编号。最初,除 <code>0</code> 号房间外的其余所有房间都被锁住。你的目标是进入所有的房间。然而,你不能在没有获得钥匙的时候进入锁住的房间。</p>
2222

23-
<p>当你进入一个房间,你可能会在里面找到一套不同的钥匙,每把钥匙上都有对应的房间号,即表示钥匙可以打开的房间。你可以拿上所有钥匙去解锁其他房间。</p>
23+
<p>当你进入一个房间,你可能会在里面找到一套 <strong>不同的钥匙</strong>,每把钥匙上都有对应的房间号,即表示钥匙可以打开的房间。你可以拿上所有钥匙去解锁其他房间。</p>
2424

2525
<p>给你一个数组 <code>rooms</code> 其中 <code>rooms[i]</code> 是你进入 <code>i</code> 号房间可以获得的钥匙集合。如果能进入 <strong>所有</strong> 房间返回 <code>true</code>,否则返回 <code>false</code>。</p>
2626

solution/0900-0999/0919.Complete Binary Tree Inserter/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tags:
2121

2222
<p><strong>完全二叉树</strong> 是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。</p>
2323

24-
<p>设计一种算法,将一个新节点插入到一个完整的二叉树中,并在插入后保持其完整。</p>
24+
<p>设计一种算法,将一个新节点插入到一棵完全二叉树中,并在插入后保持其完整。</p>
2525

2626
<p>实现 <code>CBTInserter</code> 类:</p>
2727

solution/1000-1099/1032.Stream of Characters/README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,11 @@ class StreamChecker {
202202

203203
```cpp
204204
class Trie {
205-
public:
206-
vector<Trie*> children;
207-
bool isEnd;
208-
209-
Trie()
210-
: children(26)
211-
, isEnd(false) {}
205+
private:
206+
Trie* children[26]{};
207+
bool isEnd = false;
212208

209+
public:
213210
void insert(string& w) {
214211
Trie* node = this;
215212
reverse(w.begin(), w.end());
@@ -225,7 +222,7 @@ public:
225222

226223
bool search(string& w) {
227224
Trie* node = this;
228-
for (int i = w.size() - 1; ~i; --i) {
225+
for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) {
229226
int idx = w[i] - 'a';
230227
if (!node->children[idx]) {
231228
return false;
@@ -245,7 +242,7 @@ public:
245242
string s;
246243

247244
StreamChecker(vector<string>& words) {
248-
for (auto&& w : words) {
245+
for (auto& w : words) {
249246
trie->insert(w);
250247
}
251248
}

0 commit comments

Comments
 (0)