Skip to content

Commit 5654218

Browse files
authored
feat: update lc problems (#4263)
1 parent e9446b3 commit 5654218

File tree

13 files changed

+180
-99
lines changed

13 files changed

+180
-99
lines changed

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md

+31-4
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ nums 中的所有元素都有用到,并且每一行都由不同的整数组成
6868

6969
### 方法一:数组或哈希表
7070

71-
我们先用数组或哈希表 $cnt$ 统计数组 $nums$ 中每个元素出现的次数。
71+
我们先用一个数组或者哈希表 $\textit{cnt}$ 统计数组 $\textit{nums}$ 中每个元素出现的次数。
7272

73-
然后遍历 $cnt$,对于每个元素 $x$,我们将其添加到答案列表中的第 $0$ 行,第 $1$ 行,第 $2$ 行,...,第 $cnt[x]-1$ 行。
73+
然后遍历 $\textit{cnt}$,对于每个元素 $x$,我们将其添加到答案列表中的第 $0$ 行,第 $1$ 行,第 $2$ 行,...,第 $\textit{cnt}[x]-1$ 行。
7474

7575
最后返回答案列表即可。
7676

77-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
77+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
7878

7979
<!-- tabs:start -->
8080

@@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
171171
function findMatrix(nums: number[]): number[][] {
172172
const ans: number[][] = [];
173173
const n = nums.length;
174-
const cnt: number[] = new Array(n + 1).fill(0);
174+
const cnt: number[] = Array(n + 1).fill(0);
175175
for (const x of nums) {
176176
++cnt[x];
177177
}
@@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
195+
let n = nums.len();
196+
let mut cnt = vec![0; n + 1];
197+
let mut ans: Vec<Vec<i32>> = Vec::new();
198+
199+
for &x in &nums {
200+
cnt[x as usize] += 1;
201+
}
202+
203+
for x in 1..=n as i32 {
204+
for j in 0..cnt[x as usize] {
205+
if ans.len() <= j {
206+
ans.push(Vec::new());
207+
}
208+
ans[j].push(x);
209+
}
210+
}
211+
212+
ans
213+
}
214+
}
215+
```
216+
190217
<!-- tabs:end -->
191218

192219
<!-- solution:end -->

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ It can be shown that we cannot have less than 3 rows in a valid array.</pre>
6868

6969
### Solution 1: Array or Hash Table
7070

71-
We use an array or hash table $cnt$ to count the number of occurrences of each element in the array $nums$.
71+
We first use an array or hash table $\textit{cnt}$ to count the frequency of each element in the array $\textit{nums}$.
7272

73-
Then we traverse the $cnt$ array, add $x$ to the $0$th row, the $1$st row, the $2$nd row, ..., the ($cnt[x]-1$)th row of the answer list.
73+
Then we iterate through $\textit{cnt}$. For each element $x$, we add it to the 0th row, 1st row, 2nd row, ..., and $(cnt[x]-1)$th row of the answer list.
7474

75-
Finally, return the answer list.
75+
Finally, we return the answer list.
7676

77-
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{nums}$.
7878

7979
<!-- tabs:start -->
8080

@@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
171171
function findMatrix(nums: number[]): number[][] {
172172
const ans: number[][] = [];
173173
const n = nums.length;
174-
const cnt: number[] = new Array(n + 1).fill(0);
174+
const cnt: number[] = Array(n + 1).fill(0);
175175
for (const x of nums) {
176176
++cnt[x];
177177
}
@@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
187187
}
188188
```
189189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
195+
let n = nums.len();
196+
let mut cnt = vec![0; n + 1];
197+
let mut ans: Vec<Vec<i32>> = Vec::new();
198+
199+
for &x in &nums {
200+
cnt[x as usize] += 1;
201+
}
202+
203+
for x in 1..=n as i32 {
204+
for j in 0..cnt[x as usize] {
205+
if ans.len() <= j {
206+
ans.push(Vec::new());
207+
}
208+
ans[j].push(x);
209+
}
210+
}
211+
212+
ans
213+
}
214+
}
215+
```
216+
190217
<!-- tabs:end -->
191218

192219
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
3+
let n = nums.len();
4+
let mut cnt = vec![0; n + 1];
5+
let mut ans: Vec<Vec<i32>> = Vec::new();
6+
7+
for &x in &nums {
8+
cnt[x as usize] += 1;
9+
}
10+
11+
for x in 1..=n as i32 {
12+
for j in 0..cnt[x as usize] {
13+
if ans.len() <= j {
14+
ans.push(Vec::new());
15+
}
16+
ans[j].push(x);
17+
}
18+
}
19+
20+
ans
21+
}
22+
}

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function findMatrix(nums: number[]): number[][] {
22
const ans: number[][] = [];
33
const n = nums.length;
4-
const cnt: number[] = new Array(n + 1).fill(0);
4+
const cnt: number[] = Array(n + 1).fill(0);
55
for (const x of nums) {
66
++cnt[x];
77
}

solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<strong>输入:</strong>nums = [1,10,3,4,19]
4242
<strong>输出:</strong>133
4343
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
44-
可以证明不存在值大于 133 的有序下标三元组。
44+
可以证明不存在值大于 133 的有序下标三元组。
4545
</pre>
4646

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

solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<strong>Input:</strong> nums = [12,6,1,2,7]
3232
<strong>Output:</strong> 77
3333
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
34-
It can be shown that there are no ordered triplets of indices with a value greater than 77.
34+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
3535
</pre>
3636

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

solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<strong>输入:</strong>nums = [1,10,3,4,19]
4242
<strong>输出:</strong>133
4343
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
44-
可以证明不存在值大于 133 的有序下标三元组。
44+
可以证明不存在值大于 133 的有序下标三元组。
4545
</pre>
4646

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

solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<strong>Input:</strong> nums = [12,6,1,2,7]
3232
<strong>Output:</strong> 77
3333
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
34-
It can be shown that there are no ordered triplets of indices with a value greater than 77.
34+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
3535
</pre>
3636

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

solution/3400-3499/3481.Apply Substitutions/README.md

+28-26
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,70 @@ tags:
1414

1515
<!-- problem:start -->
1616

17-
# [3481. Apply Substitutions 🔒](https://leetcode.cn/problems/apply-substitutions)
17+
# [3481. 应用替换 🔒](https://leetcode.cn/problems/apply-substitutions)
1818

1919
[English Version](/solution/3400-3499/3481.Apply%20Substitutions/README_EN.md)
2020

2121
## 题目描述
2222

2323
<!-- description:start -->
2424

25-
<p data-end="384" data-start="34">You are given a <code>replacements</code> mapping and a <code>text</code> string that may contain <strong>placeholders</strong> formatted as <code data-end="139" data-start="132">%var%</code>, where each <code>var</code> corresponds to a key in the <code>replacements</code> mapping. Each replacement value may itself contain <strong>one or more</strong> such <strong>placeholders</strong>. Each <strong>placeholder</strong> is replaced by the value associated with its corresponding replacement key.</p>
25+
<p data-end="384" data-start="34">给定一个&nbsp;<code>replacements</code>&nbsp;映射和一个可能包含格式为 <code>%var%</code> <strong>占位符&nbsp;</strong>的字符串&nbsp;<code>text</code>,其中每个&nbsp;<code>var</code>&nbsp;对应&nbsp;<code>replacements</code>&nbsp;中的一个键。每个替换值本身可能包含 <strong>一个或多个</strong> 此类<strong>占位符</strong>。每个 <strong>占位符</strong> 都被与其相应的替换键对应的值替换。</p>
2626

27-
<p data-end="353" data-start="34">Return the fully substituted <code>text</code> string which <strong>does not</strong> contain any <strong>placeholders</strong>.</p>
27+
<p data-end="353" data-start="34">返回完全替换后 <strong>不</strong> 含任何 <strong>占位符</strong> &nbsp;<code>text</code>&nbsp;字符串。</p>
2828

2929
<p>&nbsp;</p>
30-
<p><strong class="example">Example 1:</strong></p>
30+
31+
<p><strong class="example">示例 1:</strong></p>
3132

3233
<div class="example-block">
33-
<p><strong>Input:</strong> <span class="example-io">replacements = [[&quot;A&quot;,&quot;abc&quot;],[&quot;B&quot;,&quot;def&quot;]], text = &quot;%A%_%B%&quot;</span></p>
34+
<p><span class="example-io"><b>输入:</b>replacements = [["A","abc"],["B","def"]], text = "%A%_%B%"</span></p>
3435

35-
<p><strong>Output:</strong> <span class="example-io">&quot;abc_def&quot;</span></p>
36+
<p><strong>输出:</strong><span class="example-io">"abc_def"</span></p>
3637

37-
<p><strong>Explanation:</strong></p>
38+
<p><strong>解释:</strong></p>
3839

3940
<ul data-end="238" data-start="71">
40-
<li data-end="138" data-start="71">The mapping associates <code data-end="101" data-start="96">&quot;A&quot;</code> with <code data-end="114" data-start="107">&quot;abc&quot;</code> and <code data-end="124" data-start="119">&quot;B&quot;</code> with <code data-end="137" data-start="130">&quot;def&quot;</code>.</li>
41-
<li data-end="203" data-start="139">Replace <code data-end="154" data-start="149">%A%</code> with <code data-end="167" data-start="160">&quot;abc&quot;</code> and <code data-end="177" data-start="172">%B%</code> with <code data-end="190" data-start="183">&quot;def&quot;</code> in the text.</li>
42-
<li data-end="238" data-start="204">The final text becomes <code data-end="237" data-start="226">&quot;abc_def&quot;</code>.</li>
41+
<li data-end="138" data-start="71">映射将&nbsp;<code data-end="101" data-start="96">"A"</code> 与&nbsp;<code data-end="114" data-start="107">"abc"</code>&nbsp;关联,并将&nbsp;<code data-end="124" data-start="119">"B"</code> 与&nbsp;<code data-end="137" data-start="130">"def"</code>&nbsp;关联。</li>
42+
<li data-end="203" data-start="139">用&nbsp;<code data-end="167" data-start="160">"abc"</code>&nbsp;替换文本中的&nbsp;<code data-end="154" data-start="149">%A%</code>,并用&nbsp;<code data-end="190" data-start="183">"def"</code>&nbsp;替换文本中的&nbsp;<code data-end="177" data-start="172">%B%</code></li>
43+
<li data-end="238" data-start="204">最终文本变为&nbsp;<code data-end="237" data-start="226">"abc_def"</code></li>
4344
</ul>
4445
</div>
4546

46-
<p><strong class="example">Example 2:</strong></p>
47+
<p><strong class="example">示例 2:</strong></p>
4748

4849
<div class="example-block">
49-
<p><strong>Input:</strong> <span class="example-io">replacements = [[&quot;A&quot;,&quot;bce&quot;],[&quot;B&quot;,&quot;ace&quot;],[&quot;C&quot;,&quot;abc%B%&quot;]], text = &quot;%A%_%B%_%C%&quot;</span></p>
50+
<p><span class="example-io"><b>输入:</b>replacements = [["A","bce"],["B","ace"],["C","abc%B%"]], text = "%A%_%B%_%C%"</span></p>
5051

51-
<p><strong>Output:</strong> <span class="example-io">&quot;bce_ace_abcace&quot;</span></p>
52+
<p><span class="example-io"><b>输出:</b>"bce_ace_abcace"</span></p>
5253

53-
<p><strong>Explanation:</strong></p>
54+
<p><strong>解释:</strong></p>
5455

5556
<ul data-end="541" data-is-last-node="" data-is-only-node="" data-start="255">
56-
<li data-end="346" data-start="255">The mapping associates <code data-end="285" data-start="280">&quot;A&quot;</code> with <code data-end="298" data-start="291">&quot;bce&quot;</code>, <code data-end="305" data-start="300">&quot;B&quot;</code> with <code data-end="318" data-start="311">&quot;ace&quot;</code>, and <code data-end="329" data-start="324">&quot;C&quot;</code> with <code data-end="345" data-start="335">&quot;abc%B%&quot;</code>.</li>
57-
<li data-end="411" data-start="347">Replace <code data-end="362" data-start="357">%A%</code> with <code data-end="375" data-start="368">&quot;bce&quot;</code> and <code data-end="385" data-start="380">%B%</code> with <code data-end="398" data-start="391">&quot;ace&quot;</code> in the text.</li>
58-
<li data-end="496" data-start="412">Then, for <code data-end="429" data-start="424">%C%</code>, substitute <code data-end="447" data-start="442">%B%</code> in <code data-end="461" data-start="451">&quot;abc%B%&quot;</code> with <code data-end="474" data-start="467">&quot;ace&quot;</code> to obtain <code data-end="495" data-start="485">&quot;abcace&quot;</code>.</li>
59-
<li data-end="541" data-is-last-node="" data-start="497">The final text becomes <code data-end="540" data-start="522">&quot;bce_ace_abcace&quot;</code>.</li>
57+
<li data-end="346" data-start="255">映射将&nbsp;<code data-end="285" data-start="280">"A"</code> 与&nbsp;<code data-end="298" data-start="291">"bce"</code>&nbsp;关联,<code data-end="305" data-start="300">"B"</code> 与&nbsp;<code data-end="318" data-start="311">"ace"</code>&nbsp;关联,以及&nbsp;<code data-end="329" data-start="324">"C"</code> 与&nbsp;<code data-end="345" data-start="335">"abc%B%"</code>&nbsp;关联。</li>
58+
<li data-end="411" data-start="347">用&nbsp;<code data-end="375" data-start="368">"bce"</code>&nbsp;替换文本中的&nbsp;<code data-end="362" data-start="357">%A%</code>,并用&nbsp;<code data-end="398" data-start="391">"ace"</code>&nbsp;替换文本中的&nbsp;<code data-end="385" data-start="380">%B%</code></li>
59+
<li data-end="496" data-start="412">接着,对于&nbsp;<code data-end="429" data-start="424">%C%</code>,用&nbsp;<code data-end="474" data-start="467">"ace"</code> 替换&nbsp;<code data-end="461" data-start="451">"abc%B%"</code>&nbsp;中的&nbsp;<code data-end="447" data-start="442">%B%</code>&nbsp;来得到&nbsp;<code data-end="495" data-start="485">"abcace"</code></li>
60+
<li data-end="541" data-is-last-node="" data-start="497">最终文本变为&nbsp;<code data-end="540" data-start="522">"bce_ace_abcace"</code></li>
6061
</ul>
6162
</div>
6263

6364
<p>&nbsp;</p>
64-
<p><strong>Constraints:</strong></p>
65+
66+
<p><strong>提示:</strong></p>
6567

6668
<ul>
6769
<li data-end="1432" data-start="1398"><code>1 &lt;= replacements.length &lt;= 10</code></li>
68-
<li data-end="1683" data-start="1433">Each element of <code data-end="1465" data-start="1451">replacements</code> is a two-element list <code data-end="1502" data-start="1488">[key, value]</code>, where:
70+
<li data-end="1683" data-start="1433"><code data-end="1465" data-start="1451">replacements</code>&nbsp;中的每个元素是一个双值列表&nbsp;<code data-end="1502" data-start="1488">[key, value]</code>,其中:
6971
<ul data-end="1683" data-start="1513">
70-
<li data-end="1558" data-start="1513"><code data-end="1520" data-start="1515">key</code> is a single uppercase English letter.</li>
71-
<li data-end="1683" data-start="1561"><code data-end="1570" data-start="1563">value</code> is a non-empty string of at most 8 characters that may contain zero or more placeholders formatted as <code data-end="1682" data-start="1673">%&lt;key&gt;%</code>.</li>
72+
<li data-end="1558" data-start="1513"><code data-end="1520" data-start="1515">key</code>&nbsp;是一个大写英语字母。</li>
73+
<li data-end="1683" data-start="1561"><code data-end="1570" data-start="1563">value</code>&nbsp;是一个最多有 8 个字符,可能包含 0 个或更多格式为&nbsp;<code data-end="1682" data-start="1673">%&lt;key&gt;%</code> 的占位符的非空字符串。</li>
7274
</ul>
7375
</li>
74-
<li data-end="726" data-start="688">All replacement keys are unique.</li>
75-
<li data-end="1875" data-start="1723">The <code>text</code> string is formed by concatenating all key placeholders (formatted as <code data-end="1808" data-start="1799">%&lt;key&gt;%</code>) randomly from the replacements mapping, separated by underscores.</li>
76+
<li data-end="726" data-start="688">所有的替换键互不相同。</li>
77+
<li data-end="1875" data-start="1723"><code>text</code>&nbsp;字符串是通过从替换映射中随机串联所有 key 占位符(格式为 <code>%&lt;key&gt;%</code>)而形成的,以虚线分隔。</li>
7678
<li data-end="1942" data-start="1876"><code>text.length == 4 * replacements.length - 1</code></li>
77-
<li data-end="2052" data-start="1943">Every placeholder in the <code>text</code> or in any replacement value corresponds to a key in the <code>replacements</code> mapping.</li>
78-
<li data-end="2265" data-start="2205">There are no cyclic dependencies between replacement keys.</li>
79+
<li data-end="2052" data-start="1943"><code>text</code>&nbsp;或任何替换值中的每个占位符对应&nbsp;<code>replacements</code> 映射中的一个键。</li>
80+
<li data-end="2265" data-start="2205">替换键之间没有循环依赖。</li>
7981
</ul>
8082

8183
<!-- description:end -->

0 commit comments

Comments
 (0)