Skip to content

Commit 45c7437

Browse files
authored
feat: update lc problems (#3195)
1 parent 4b6d5d4 commit 45c7437

File tree

38 files changed

+774
-316
lines changed

38 files changed

+774
-316
lines changed

lcof2/剑指 Offer II 065. 最短的单词编码/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class Trie {
228228
class Solution {
229229
func minimumLengthEncoding(_ words: [String]) -> Int {
230230
let root = Trie()
231-
231+
232232
for word in words {
233233
var current = root
234234
for char in word.reversed() {
@@ -239,25 +239,25 @@ class Solution {
239239
current = current.children[index]!
240240
}
241241
}
242-
242+
243243
return dfs(root, 1)
244244
}
245-
245+
246246
private func dfs(_ current: Trie, _ length: Int) -> Int {
247247
var isLeaf = true
248248
var result = 0
249-
249+
250250
for child in current.children {
251251
if let child = child {
252252
isLeaf = false
253253
result += dfs(child, length + 1)
254254
}
255255
}
256-
256+
257257
if isLeaf {
258258
result += length
259259
}
260-
260+
261261
return result
262262
}
263263
}

solution/0200-0299/0251.Flatten 2D Vector/README.md

+35-19
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,50 @@ tags:
1919

2020
<!-- description:start -->
2121

22-
<p>请设计并实现一个能够展开二维向量的迭代器。该迭代器需要支持 <code>next</code> 和 <code>hasNext</code> 两种操作。</p>
22+
<p>请设计并实现一个能够展开二维向量的迭代器。该迭代器需要支持&nbsp;<code>next</code> 和&nbsp;<code>hasNext</code>&nbsp;两种操作。</p>
2323

24-
<p> </p>
24+
<p>实现&nbsp;<code>Vector2D</code>&nbsp;类:</p>
2525

26-
<p><strong>示例:</strong></p>
26+
<ul>
27+
<li><code>Vector2D(int[][] vec)</code>&nbsp;使用二维向量&nbsp;<code>vec</code>&nbsp;初始化对象</li>
28+
<li><code>next()</code>&nbsp;从二维向量返回下一个元素并将指针移动到下一个位置。你可以假设对&nbsp;<code>next</code>&nbsp;的所有调用都是合法的。</li>
29+
<li><code>hasNext()</code>&nbsp;当向量中还有元素返回&nbsp;<code>true</code>,否则返回 <code>false</code>。</li>
30+
</ul>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong>示例 1:</strong></p>
2735

2836
<pre>
29-
Vector2D iterator = new Vector2D([[1,2],[3],[4]]);
30-
31-
iterator.next(); // 返回 1
32-
iterator.next(); // 返回 2
33-
iterator.next(); // 返回 3
34-
iterator.hasNext(); // 返回 true
35-
iterator.hasNext(); // 返回 true
36-
iterator.next(); // 返回 4
37-
iterator.hasNext(); // 返回 false
37+
<strong>输入:</strong>
38+
["Vector2D", "next", "next", "next", "hasNext", "hasNext", "next", "hasNext"]
39+
[[[[1, 2], [3], [4]]], [], [], [], [], [], [], []]
40+
<strong>输出:</strong>
41+
[null, 1, 2, 3, true, true, 4, false]
42+
43+
<strong>解释:</strong>
44+
Vector2D vector2D = new Vector2D([[1, 2], [3], [4]]);
45+
vector2D.next(); // return 1
46+
vector2D.next(); // return 2
47+
vector2D.next(); // return 3
48+
vector2D.hasNext(); // return True
49+
vector2D.hasNext(); // return True
50+
vector2D.next(); // return 4
51+
vector2D.hasNext(); // return False
3852
</pre>
3953

40-
<p> </p>
54+
<p>&nbsp;</p>
4155

42-
<p><strong>注意:</strong></p>
56+
<p><b>提示:</b></p>
4357

44-
<ol>
45-
<li>请记得 <strong>重置 </strong>在 Vector2D 中声明的类变量(静态变量),因为类变量会 <strong>在多个测试用例中保持不变</strong>,影响判题准确。请 <a href="https://support.leetcode.cn/hc/kb/section/1071534/" target="_blank">查阅</a> 这里。</li>
46-
<li>你可以假定 <code>next()</code> 的调用总是合法的,即当 <code>next()</code> 被调用时,二维向量总是存在至少一个后续元素。</li>
47-
</ol>
58+
<ul>
59+
<li><code>0 &lt;= vec.length &lt;= 200</code></li>
60+
<li><code>0 &lt;= vec[i].length &lt;= 500</code></li>
61+
<li><code>-500 &lt;= vec[i][j] &lt;= 500</code></li>
62+
<li>最多调用&nbsp;<code>next</code> 和&nbsp;<code>hasNext</code>&nbsp;<code>10<sup>5</sup></code>&nbsp;次。</li>
63+
</ul>
4864

49-
<p> </p>
65+
<p>&nbsp;</p>
5066

5167
<p><strong>进阶:</strong>尝试在代码中仅使用 <a href="http://www.cplusplus.com/reference/iterator/iterator/">C++ 提供的迭代器</a> 或 <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html">Java 提供的迭代器</a>。</p>
5268

solution/0200-0299/0271.Encode and Decode Strings/README.md

+45-8
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,78 @@ tags:
2222

2323
<p>1 号机(发送方)有如下函数:</p>
2424

25-
<pre>string encode(vector&lt;string&gt; strs) {
25+
<pre>
26+
string encode(vector&lt;string&gt; strs) {
2627
// ... your code
2728
return encoded_string;
2829
}</pre>
2930

3031
<p>2 号机(接收方)有如下函数:</p>
3132

32-
<pre>vector&lt;string&gt; decode(string s) {
33+
<pre>
34+
vector&lt;string&gt; decode(string s) {
3335
//... your code
3436
return strs;
3537
}
3638
</pre>
3739

3840
<p>1 号机(发送方)执行:</p>
3941

40-
<pre>string encoded_string = encode(strs);
42+
<pre>
43+
string encoded_string = encode(strs);
4144
</pre>
4245

4346
<p>2 号机(接收方)执行:</p>
4447

45-
<pre>vector&lt;string&gt; strs2 = decode(encoded_string);
48+
<pre>
49+
vector&lt;string&gt; strs2 = decode(encoded_string);
4650
</pre>
4751

4852
<p>此时,2 号机(接收方)的 <code>strs2</code>&nbsp;需要和 1 号机(发送方)的 <code>strs</code> 相同。</p>
4953

5054
<p>请你来实现这个&nbsp;<code>encode</code> 和&nbsp;<code>decode</code> 方法。</p>
5155

52-
<p><strong>注意:</strong></p>
56+
<p>不允许使用任何序列化方法解决这个问题(例如 <code>eval</code>)。</p>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong class="example">示例 1:</strong></p>
61+
62+
<pre>
63+
<b>输入:</b>dummy_input = ["Hello","World"]
64+
<b>输出:</b>["Hello","World"]
65+
<strong>解释:</strong>
66+
1 号机:
67+
Codec encoder = new Codec();
68+
String msg = encoder.encode(strs);
69+
Machine 1 ---msg---&gt; Machine 2
70+
71+
2 号机:
72+
Codec decoder = new Codec();
73+
String[] strs = decoder.decode(msg);
74+
</pre>
75+
76+
<p><strong class="example">示例 2:</strong></p>
77+
78+
<pre>
79+
<b>输入:</b>dummy_input = [""]
80+
<b>输出:</b>[""]
81+
</pre>
82+
83+
<p>&nbsp;</p>
84+
85+
<p><strong>提示:</strong></p>
5386

5487
<ul>
55-
<li>因为字符串可能会包含 256 个合法&nbsp;ascii 字符中的任何字符,所以您的算法必须要能够处理任何可能会出现的字符。</li>
56-
<li>请勿使用 &ldquo;类成员&rdquo;、&ldquo;全局变量&rdquo; 或 &ldquo;静态变量&rdquo; 来存储这些状态,您的编码和解码算法应该是非状态依赖的。</li>
57-
<li>请不要依赖任何方法库,例如 <code>eval</code>&nbsp;又或者是&nbsp;<code>serialize</code>&nbsp;之类的方法。本题的宗旨是需要您自己实现 &ldquo;编码&rdquo; 和 &ldquo;解码&rdquo; 算法。</li>
88+
<li><code>1 &lt;= strs.length &lt;= 200</code></li>
89+
<li><code>0 &lt;= strs[i].length &lt;= 200</code></li>
90+
<li><code>strs[i]</code>&nbsp;包含 256 个有效 ASCII 字符中的任何可能字符。</li>
5891
</ul>
5992

93+
<p>&nbsp;</p>
94+
95+
<p><strong>进阶:</strong>你能编写一个通用算法来处理任何可能的字符集吗?</p>
96+
6097
<!-- description:end -->
6198

6299
## 解法

solution/0500-0599/0553.Optimal Division/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818

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

21-
<p>给定一正整数数组<strong> </strong><code>nums</code><strong>,</strong><code>nums</code> 中的相邻整数将进行浮点除法。例如,&nbsp;[2,3,4] -&gt; 2 / 3 / 4 。</p>
21+
<p>给定一正整数数组<strong> </strong><code>nums</code><strong>,</strong><code>nums</code> 中的相邻整数将进行浮点除法。</p>
2222

2323
<ul>
2424
<li>例如,<code>nums = [2,3,4]</code>,我们将求表达式的值&nbsp;<code>"2/3/4"</code>。</li>

solution/0700-0799/0720.Longest Word in Dictionary/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ tags:
2424

2525
<p>若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。</p>
2626

27+
<p>请注意,单词应该从左到右构建,每个额外的字符都添加到前一个单词的结尾。</p>
28+
2729
<p>&nbsp;</p>
2830

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

solution/1200-1299/1236.Web Crawler/README.md

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

2020
<!-- description:start -->
2121

22-
<p>给定一个链接&nbsp;<code>startUrl</code> 和一个接口&nbsp;<code>HtmlParser</code>&nbsp;,请你实现一个网络爬虫,以实现爬取同&nbsp;<code>startUrl</code>&nbsp;拥有相同&nbsp;<strong>域名标签&nbsp;</strong>的全部链接。</p>
22+
<p>给定一个链接&nbsp;<code>startUrl</code> 和一个接口&nbsp;<code>HtmlParser</code>&nbsp;,请你实现一个网络爬虫,以实现爬取同&nbsp;<code>startUrl</code>&nbsp;拥有相同&nbsp;<strong>主机名&nbsp;</strong>的全部链接。</p>
2323

2424
<p>该爬虫得到的全部链接可以&nbsp;<strong>任何顺序&nbsp;</strong>返回结果。</p>
2525

@@ -98,8 +98,8 @@ startUrl = "http://news.google.com"
9898
<li><code>1 &lt;= urls.length &lt;= 1000</code></li>
9999
<li><code>1 &lt;= urls[i].length &lt;= 300</code></li>
100100
<li><code>startUrl</code>&nbsp;为&nbsp;<code>urls</code>&nbsp;中的一个。</li>
101-
<li>域名标签的长为1到63个字符(包括点),只能包含从‘a’到‘z’的ASCII字母、‘0’到‘9’的数字以及连字符即减号(‘-’)。</li>
102-
<li>域名标签不会以连字符即减号(‘-’)开头或结尾。</li>
101+
<li>主机名的长为1到63个字符(包括点),只能包含从‘a’到‘z’的ASCII字母、‘0’到‘9’的数字以及连字符即减号(‘-’)。</li>
102+
<li>主机名不会以连字符即减号(‘-’)开头或结尾。</li>
103103
<li>关于域名有效性的约束可参考:&nbsp;&nbsp;<a href="https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames">https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames</a></li>
104104
<li>你可以假定url库中不包含重复项。</li>
105105
</ul>

solution/1200-1299/1240.Tiling a Rectangle with the Fewest Squares/README.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ tags:
1818

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

21-
<p>你是一位施工队的工长,根据设计师的要求准备为一套设计风格独特的房子进行室内装修。</p>
22-
23-
<p>房子的客厅大小为&nbsp;<code>n</code>&nbsp;x <code>m</code>,为保持极简的风格,需要使用尽可能少的 <strong>正方形</strong> 瓷砖来铺盖地面。</p>
24-
25-
<p>假设正方形瓷砖的规格不限,边长都是整数。</p>
26-
27-
<p>请你帮设计师计算一下,最少需要用到多少块方形瓷砖?</p>
21+
<p>给定一个大小为&nbsp;<code>n</code>&nbsp;x&nbsp;<code>m</code>&nbsp;的长方形,返回贴满矩形所需的整数边正方形的最小数量。</p>
2822

2923
<p>&nbsp;</p>
3024

@@ -35,9 +29,9 @@ tags:
3529
<pre>
3630
<strong>输入:</strong>n = 2, m = 3
3731
<strong>输出:</strong>3
38-
<code><strong>解释:</strong>3</code> 块地砖就可以铺满卧室
39-
<code> 2</code> <code>1x1 地砖</code>
40-
<code> 1</code> <code>2x2 地砖</code></pre>
32+
<code><strong>解释:</strong>需要<strong> </strong>3</code> 个正方形来覆盖长方形
33+
<code> 2</code> <code>1x1 的正方形</code>
34+
<code> 1</code> <code>2x2 的正方形</code></pre>
4135

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

solution/1700-1799/1797.Design Authentication Manager/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ source: 第 48 场双周赛 Q2
77
tags:
88
- 设计
99
- 哈希表
10+
- 链表
11+
- 双向链表
1012
---
1113

1214
<!-- problem:start -->

solution/1700-1799/1797.Design Authentication Manager/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ source: Biweekly Contest 48 Q2
77
tags:
88
- Design
99
- Hash Table
10+
- Linked List
11+
- Doubly-Linked List
1012
---
1113

1214
<!-- problem:start -->

solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818

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

21-
<p>给定一个由整数数组组成的数组&nbsp;<code>arrays</code>,其中&nbsp;<code>arrays[i]</code>&nbsp;是 <strong>严格递增</strong> 排序的,返回一个表示 <strong>所有</strong> 数组之间的 <strong>最长公共子序列</strong> 的整数数组。</p>
21+
<p>给定一个由整数数组组成的数组&nbsp;<code>arrays</code>,其中&nbsp;<code>arrays[i]</code>&nbsp;是 <strong>严格递增</strong> 排序的,返回一个 <strong>所有</strong> 数组均包含的 <strong>最长公共子序列</strong> 的整数数组。</p>
2222

2323
<p><strong>子序列</strong> 是从另一个序列派生出来的序列,删除一些元素或不删除任何元素,而不改变其余元素的顺序。</p>
2424

solution/2800-2899/2806.Account Balance After Rounded Purchase/README_EN.md

+39-17
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,57 @@ tags:
1818

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

21-
<p>Initially, you have a bank account balance of <code>100</code> dollars.</p>
21+
<p>Initially, you have a bank account balance of <strong>100</strong> dollars.</p>
2222

23-
<p>You are given an integer <code>purchaseAmount</code> representing the amount you will spend on a purchase in dollars.</p>
23+
<p>You are given an integer <code>purchaseAmount</code> representing the amount you will spend on a purchase in dollars, in other words, its price.</p>
2424

25-
<p>At the store where you will make the purchase, the purchase amount is rounded to the <strong>nearest multiple</strong> of <code>10</code>. In other words, you pay a <strong>non-negative</strong> amount, <code>roundedAmount</code>, such that <code>roundedAmount</code> is a multiple of <code>10</code> and <code>abs(roundedAmount - purchaseAmount)</code> is <strong>minimized</strong>.</p>
25+
<p>When making the purchase, first the <code>purchaseAmount</code> <strong>is rounded to the nearest multiple of 10</strong>. Let us call this value <code>roundedAmount</code>. Then, <code>roundedAmount</code> dollars are removed from your bank account.</p>
2626

27-
<p>If there is more than one nearest multiple of <code>10</code>, the <strong>largest multiple</strong> is chosen.</p>
27+
<p>Return an integer denoting your final bank account balance after this purchase.</p>
2828

29-
<p>Return <em>an integer denoting your account balance after making a purchase worth </em><code>purchaseAmount</code><em> dollars from the store.</em></p>
29+
<p><strong>Notes:</strong></p>
3030

31-
<p><strong>Note:</strong> <code>0</code> is considered to be a multiple of <code>10</code> in this problem.</p>
31+
<ul>
32+
<li>0 is considered to be a multiple of 10 in this problem.</li>
33+
<li>When rounding, 5 is rounded upward (5 is rounded to 10, 15 is rounded to 20, 25 to 30, and so on).</li>
34+
</ul>
3235

3336
<p>&nbsp;</p>
3437
<p><strong class="example">Example 1:</strong></p>
3538

36-
<pre>
37-
<strong>Input:</strong> purchaseAmount = 9
38-
<strong>Output:</strong> 90
39-
<strong>Explanation:</strong> In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
40-
</pre>
39+
<div class="example-block">
40+
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 9</span></p>
41+
42+
<p><strong>Output:</strong> <span class="example-io">90</span></p>
43+
44+
<p><strong>Explanation:</strong></p>
45+
46+
<p>The nearest multiple of 10 to 9 is 10. So your account balance becomes 100 - 10 = 90.</p>
47+
</div>
4148

4249
<p><strong class="example">Example 2:</strong></p>
4350

44-
<pre>
45-
<strong>Input:</strong> purchaseAmount = 15
46-
<strong>Output:</strong> 80
47-
<strong>Explanation:</strong> In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen.
48-
Hence, your account balance becomes 100 - 20 = 80.
49-
</pre>
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 15</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">80</span></p>
55+
56+
<p><strong>Explanation:</strong></p>
57+
58+
<p>The nearest multiple of 10 to 15 is 20. So your account balance becomes 100 - 20 = 80.</p>
59+
</div>
60+
61+
<p><strong class="example">Example 3:</strong></p>
62+
63+
<div class="example-block">
64+
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 10</span></p>
65+
66+
<p><strong>Output:</strong> <span class="example-io">90</span></p>
67+
68+
<p><strong>Explanation:</strong></p>
69+
70+
<p>10 is a multiple of 10 itself. So your account balance becomes 100 - 10 = 90.</p>
71+
</div>
5072

5173
<p>&nbsp;</p>
5274
<p><strong>Constraints:</strong></p>

0 commit comments

Comments
 (0)