Skip to content

feat: update lc problems #3195

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
Jul 3, 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
12 changes: 6 additions & 6 deletions lcof2/剑指 Offer II 065. 最短的单词编码/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Trie {
class Solution {
func minimumLengthEncoding(_ words: [String]) -> Int {
let root = Trie()

for word in words {
var current = root
for char in word.reversed() {
Expand All @@ -239,25 +239,25 @@ class Solution {
current = current.children[index]!
}
}

return dfs(root, 1)
}

private func dfs(_ current: Trie, _ length: Int) -> Int {
var isLeaf = true
var result = 0

for child in current.children {
if let child = child {
isLeaf = false
result += dfs(child, length + 1)
}
}

if isLeaf {
result += length
}

return result
}
}
Expand Down
54 changes: 35 additions & 19 deletions solution/0200-0299/0251.Flatten 2D Vector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,50 @@ tags:

<!-- description:start -->

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

<p> </p>
<p>实现&nbsp;<code>Vector2D</code>&nbsp;类:</p>

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

<p>&nbsp;</p>

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

<pre>
Vector2D iterator = new Vector2D([[1,2],[3],[4]]);

iterator.next(); // 返回 1
iterator.next(); // 返回 2
iterator.next(); // 返回 3
iterator.hasNext(); // 返回 true
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 4
iterator.hasNext(); // 返回 false
<strong>输入:</strong>
["Vector2D", "next", "next", "next", "hasNext", "hasNext", "next", "hasNext"]
[[[[1, 2], [3], [4]]], [], [], [], [], [], [], []]
<strong>输出:</strong>
[null, 1, 2, 3, true, true, 4, false]

<strong>解释:</strong>
Vector2D vector2D = new Vector2D([[1, 2], [3], [4]]);
vector2D.next(); // return 1
vector2D.next(); // return 2
vector2D.next(); // return 3
vector2D.hasNext(); // return True
vector2D.hasNext(); // return True
vector2D.next(); // return 4
vector2D.hasNext(); // return False
</pre>

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

<p><strong>注意:</strong></p>
<p><b>提示:</b></p>

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

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

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

Expand Down
53 changes: 45 additions & 8 deletions solution/0200-0299/0271.Encode and Decode Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,78 @@ tags:

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

<pre>string encode(vector&lt;string&gt; strs) {
<pre>
string encode(vector&lt;string&gt; strs) {
// ... your code
return encoded_string;
}</pre>

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

<pre>vector&lt;string&gt; decode(string s) {
<pre>
vector&lt;string&gt; decode(string s) {
//... your code
return strs;
}
</pre>

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

<pre>string encoded_string = encode(strs);
<pre>
string encoded_string = encode(strs);
</pre>

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

<pre>vector&lt;string&gt; strs2 = decode(encoded_string);
<pre>
vector&lt;string&gt; strs2 = decode(encoded_string);
</pre>

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

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

<p><strong>注意:</strong></p>
<p>不允许使用任何序列化方法解决这个问题(例如 <code>eval</code>)。</p>

<p>&nbsp;</p>

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

<pre>
<b>输入:</b>dummy_input = ["Hello","World"]
<b>输出:</b>["Hello","World"]
<strong>解释:</strong>
1 号机:
Codec encoder = new Codec();
String msg = encoder.encode(strs);
Machine 1 ---msg---&gt; Machine 2

2 号机:
Codec decoder = new Codec();
String[] strs = decoder.decode(msg);
</pre>

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

<pre>
<b>输入:</b>dummy_input = [""]
<b>输出:</b>[""]
</pre>

<p>&nbsp;</p>

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

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

<p>&nbsp;</p>

<p><strong>进阶:</strong>你能编写一个通用算法来处理任何可能的字符集吗?</p>

<!-- description:end -->

## 解法
Expand Down
2 changes: 1 addition & 1 deletion solution/0500-0599/0553.Optimal Division/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:

<!-- description:start -->

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

<ul>
<li>例如,<code>nums = [2,3,4]</code>,我们将求表达式的值&nbsp;<code>"2/3/4"</code>。</li>
Expand Down
2 changes: 2 additions & 0 deletions solution/0700-0799/0720.Longest Word in Dictionary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ tags:

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

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

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
Expand Down
6 changes: 3 additions & 3 deletions solution/1200-1299/1236.Web Crawler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

<!-- description:start -->

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

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

Expand Down Expand Up @@ -98,8 +98,8 @@ startUrl = "http://news.google.com"
<li><code>1 &lt;= urls.length &lt;= 1000</code></li>
<li><code>1 &lt;= urls[i].length &lt;= 300</code></li>
<li><code>startUrl</code>&nbsp;为&nbsp;<code>urls</code>&nbsp;中的一个。</li>
<li>域名标签的长为1到63个字符(包括点),只能包含从‘a’到‘z’的ASCII字母、‘0’到‘9’的数字以及连字符即减号(‘-’)。</li>
<li>域名标签不会以连字符即减号(‘-’)开头或结尾。</li>
<li>主机名的长为1到63个字符(包括点),只能包含从‘a’到‘z’的ASCII字母、‘0’到‘9’的数字以及连字符即减号(‘-’)。</li>
<li>主机名不会以连字符即减号(‘-’)开头或结尾。</li>
<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>
<li>你可以假定url库中不包含重复项。</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ tags:

<!-- description:start -->

<p>你是一位施工队的工长,根据设计师的要求准备为一套设计风格独特的房子进行室内装修。</p>

<p>房子的客厅大小为&nbsp;<code>n</code>&nbsp;x <code>m</code>,为保持极简的风格,需要使用尽可能少的 <strong>正方形</strong> 瓷砖来铺盖地面。</p>

<p>假设正方形瓷砖的规格不限,边长都是整数。</p>

<p>请你帮设计师计算一下,最少需要用到多少块方形瓷砖?</p>
<p>给定一个大小为&nbsp;<code>n</code>&nbsp;x&nbsp;<code>m</code>&nbsp;的长方形,返回贴满矩形所需的整数边正方形的最小数量。</p>

<p>&nbsp;</p>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ source: 第 48 场双周赛 Q2
tags:
- 设计
- 哈希表
- 链表
- 双向链表
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ source: Biweekly Contest 48 Q2
tags:
- Design
- Hash Table
- Linked List
- Doubly-Linked List
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:

<!-- description:start -->

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,57 @@ tags:

<!-- description:start -->

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

<p>You are given an integer <code>purchaseAmount</code> representing the amount you will spend on a purchase in dollars.</p>
<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>

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

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

<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>
<p><strong>Notes:</strong></p>

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

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

<pre>
<strong>Input:</strong> purchaseAmount = 9
<strong>Output:</strong> 90
<strong>Explanation:</strong> In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
</pre>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 9</span></p>

<p><strong>Output:</strong> <span class="example-io">90</span></p>

<p><strong>Explanation:</strong></p>

<p>The nearest multiple of 10 to 9 is 10. So your account balance becomes 100 - 10 = 90.</p>
</div>

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

<pre>
<strong>Input:</strong> purchaseAmount = 15
<strong>Output:</strong> 80
<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.
Hence, your account balance becomes 100 - 20 = 80.
</pre>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 15</span></p>

<p><strong>Output:</strong> <span class="example-io">80</span></p>

<p><strong>Explanation:</strong></p>

<p>The nearest multiple of 10 to 15 is 20. So your account balance becomes 100 - 20 = 80.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">purchaseAmount = 10</span></p>

<p><strong>Output:</strong> <span class="example-io">90</span></p>

<p><strong>Explanation:</strong></p>

<p>10 is a multiple of 10 itself. So your account balance becomes 100 - 10 = 90.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
Expand Down
Loading
Loading