Skip to content

feat: update lc problems #3419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0000-0099/0001.Two Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions solution/0000-0099/0016.3Sum Closest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ tags:
<pre>
<strong>输入:</strong>nums = [-1,2,1,-4], target = 1
<strong>输出:</strong>2
<strong>解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2)
<strong>解释:</strong>与 target 最接近的和是 2 (-1 + 2 + 1 = 2)。
</pre>

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

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

<p>&nbsp;</p>

Expand Down
84 changes: 57 additions & 27 deletions solution/0000-0099/0071.Simplify Path/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ tags:

<!-- description:start -->

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

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

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

Expand All @@ -32,44 +32,74 @@ tags:

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

<p> </p>
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>

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

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

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

<p><strong>示例 3:</strong></p>
<p>应删除尾部斜杠。</p>
</div>

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

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

<pre>
<strong>输入:</strong>path = "/a/./b/../../c/"
<strong>输出:</strong>"/c"
</pre>
<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/home/foo"</span></p>

<p> </p>
<p><strong>解释:</strong></p>

<p>多个连续的斜杠被单个斜杠替换。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/home/user/Documents/../Pictures"</span></p>

<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/home/user/Pictures"</span></p>

<p><strong>解释:</strong></p>

<p>两个点&nbsp;<code>".."</code>&nbsp;表示上一级目录。</p>
</div>

<p><strong class="example">示例 4:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/../"</span></p>

<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/"</span></p>

<p><strong>解释:</strong></p>

<p>不可能从根目录上升级一级。</p>
</div>

<p><strong class="example">示例 5:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b></span><span class="example-io">path = "/.../a/../b/c/../d/./"</span></p>

<p><span class="example-io"><b>输出:</b></span><span class="example-io">"/.../b/d"</span></p>

<p><strong>解释:</strong></p>

<p><code>"..."</code> 是此问题中目录的有效名称。</p>
</div>

<p>&nbsp;</p>

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

<ul>
<li><code>1 <= path.length <= 3000</code></li>
<li><code>1 &lt;= path.length &lt;= 3000</code></li>
<li><code>path</code> 由英文字母,数字,<code>'.'</code>,<code>'/'</code> 或 <code>'_'</code> 组成。</li>
<li><code>path</code> 是一个有效的 Unix 风格绝对路径。</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tags:

<!-- problem:start -->

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tags:

<!-- problem:start -->

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

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

Expand Down
30 changes: 17 additions & 13 deletions solution/0400-0499/0457.Circular Array Loop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,33 @@ tags:

<p>&nbsp;</p>

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

<p><strong class="example">示例 1:</strong></p>
<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;" />
<pre>
<strong>输入:</strong>nums = [2,-1,1,2,2]
<strong>输出:</strong>true
<strong>解释:</strong>存在循环,按下标 0 -&gt; 2 -&gt; 3 -&gt; 0 。循环长度为 3 。
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
我们可以看到存在循环,按下标 0 -&gt; 2 -&gt; 3 -&gt; 0 --&gt; ...,并且其中的所有节点都是白色(以相同方向跳跃)。
</pre>

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

<p><strong class="example">示例 2:</strong></p>
<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;" />
<pre>
<strong>输入:</strong>nums = [-1,2]
<strong>输入:</strong>nums = [-1,-2,-3,-4,-5,6]
<strong>输出:</strong>false
<strong>解释:</strong>按下标 1 -&gt; 1 -&gt; 1 ... 的运动无法构成循环,因为循环的长度为 1 。根据定义,循环的长度必须大于 1 。
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
唯一的循环长度为 1,所以返回 false。
</pre>

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

<p><strong class="example">示例 3</strong></p>
<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;" />
<pre>
<strong>输入:</strong>nums = [-2,1,-1,-2,-2]
<strong>输出:</strong>false
<strong>解释:</strong>按下标 1 -&gt; 2 -&gt; 1 -&gt; ... 的运动无法构成循环,因为 nums[1] 是正数,而 nums[2] 是负数。
所有 nums[seq[j]] 应当不是全正就是全负。</pre>
<strong>输入:</strong>nums = [1,-1,5,1,4]
<strong>输出:</strong>true
<strong>解释:</strong>图片展示了节点间如何连接。白色节点向前跳跃,而红色节点向后跳跃。
我们可以看到存在循环,按下标 0 --&gt; 1 --&gt; 0 --&gt; ...,当它的大小大于 1 时,它有一个向前跳的节点和一个向后跳的节点,所以 <strong>它不是一个循环</strong>。
我们可以看到存在循环,按下标 3 --&gt; 4 --&gt; 3 --&gt; ...,并且其中的所有节点都是白色(以相同方向跳跃)。
</pre>

<p>&nbsp;</p>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 8 additions & 9 deletions solution/0600-0699/0638.Shopping Offers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:

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

<p> </p>
<p>&nbsp;</p>

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

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

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li><code>n == price.length</code></li>
<li><code>n == needs.length</code></li>
<li><code>1 <= n <= 6</code></li>
<li><code>0 <= price[i] <= 10</code></li>
<li><code>0 <= needs[i] <= 10</code></li>
<li><code>1 <= special.length <= 100</code></li>
<li><code>n == price.length == needs.length</code></li>
<li><code>1 &lt;= n &lt;= 6</code></li>
<li><code>0 &lt;= price[i], needs[i] &lt;= 10</code></li>
<li><code>1 &lt;= special.length &lt;= 100</code></li>
<li><code>special[i].length == n + 1</code></li>
<li><code>0 <= special[i][j] <= 50</code></li>
<li><code>0 &lt;= special[i][j] &lt;= 50</code></li>
<li>生成的输入对于&nbsp;<code>0 &lt;= j &lt;= n - 1</code> 至少有一个&nbsp;<code>special[i][j]</code>&nbsp;非零。</li>
</ul>

<!-- description:end -->
Expand Down
1 change: 1 addition & 0 deletions solution/0600-0699/0638.Shopping Offers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ You cannot add more items, though only $9 for 2A ,2B and 1C.
<li><code>1 &lt;= special.length &lt;= 100</code></li>
<li><code>special[i].length == n + 1</code></li>
<li><code>0 &lt;= special[i][j] &lt;= 50</code></li>
<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>
</ul>

<!-- description:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tags:

<!-- description:start -->

<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>
<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
40 changes: 22 additions & 18 deletions solution/0700-0799/0787.Cheapest Flights Within K Stops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,36 @@ tags:
<p>&nbsp;</p>

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

<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;" />
<pre>
<strong>输入:</strong>
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
<strong>输出:</strong> 200
<strong>解释:</strong>
城市航班图如下
<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;" />

从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。</pre>
n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
<strong>输出:</strong> 700
<strong>解释:</strong> 城市航班图如上
从城市 0 到城市 3 经过最多 1 站的最佳路径用红色标记,费用为 100 + 600 = 700。
请注意,通过城市 [0, 1, 2, 3] 的路径更便宜,但无效,因为它经过了 2 站。
</pre>

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

<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;" />
<pre>
<strong>输入:</strong>
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
<strong>输出:</strong> 500
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
<strong>输出:</strong> 200
<strong>解释:</strong>
城市航班图如下
<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;" />

从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。</pre>
城市航班图如上
从城市 0 到城市 2 经过最多 1 站的最佳路径标记为红色,费用为 100 + 100 = 200。
</pre>

<p>&nbsp;</p>
<p><strong class="example">示例 3:</strong></p>
<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;" />
<pre>
<b>输入:</b>n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
<b>输出:</b>500
<strong>解释:</strong>
城市航班图如上
从城市 0 到城市 2 不经过站点的最佳路径标记为红色,费用为 500。
</pre>

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

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion solution/0800-0899/0841.Keys and Rooms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tags:

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tags:

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

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

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

Expand Down
15 changes: 6 additions & 9 deletions solution/1000-1099/1032.Stream of Characters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,11 @@ class StreamChecker {

```cpp
class Trie {
public:
vector<Trie*> children;
bool isEnd;

Trie()
: children(26)
, isEnd(false) {}
private:
Trie* children[26]{};
bool isEnd = false;

public:
void insert(string& w) {
Trie* node = this;
reverse(w.begin(), w.end());
Expand All @@ -225,7 +222,7 @@ public:

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

StreamChecker(vector<string>& words) {
for (auto&& w : words) {
for (auto& w : words) {
trie->insert(w);
}
}
Expand Down
Loading
Loading