Skip to content
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

feat: update lc problems #4263

Merged
merged 1 commit into from
Mar 19, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -68,13 +68,13 @@ nums 中的所有元素都有用到,并且每一行都由不同的整数组成

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

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

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

最后返回答案列表即可。

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

<!-- tabs:start -->

@@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
@@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Original file line number Diff line number Diff line change
@@ -68,13 +68,13 @@ It can be shown that we cannot have less than 3 rows in a valid array.</pre>

### Solution 1: Array or Hash Table

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

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

Finally, return the answer list.
Finally, we return the answer list.

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

<!-- tabs:start -->

@@ -171,7 +171,7 @@ func findMatrix(nums []int) (ans [][]int) {
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
@@ -187,6 +187,33 @@ function findMatrix(nums: number[]): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
let n = nums.len();
let mut cnt = vec![0; n + 1];
let mut ans: Vec<Vec<i32>> = Vec::new();

for &x in &nums {
cnt[x as usize] += 1;
}

for x in 1..=n as i32 {
for j in 0..cnt[x as usize] {
if ans.len() <= j {
ans.push(Vec::new());
}
ans[j].push(x);
}
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function findMatrix(nums: number[]): number[][] {
const ans: number[][] = [];
const n = nums.length;
const cnt: number[] = new Array(n + 1).fill(0);
const cnt: number[] = Array(n + 1).fill(0);
for (const x of nums) {
++cnt[x];
}
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ tags:
<strong>输入:</strong>nums = [1,10,3,4,19]
<strong>输出:</strong>133
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
可以证明不存在值大于 133 的有序下标三元组。
可以证明不存在值大于 133 的有序下标三元组。
</pre>

<p><strong class="example">示例 3:</strong></p>
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ tags:
<strong>Input:</strong> nums = [12,6,1,2,7]
<strong>Output:</strong> 77
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
</pre>

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

<p><strong class="example">示例 3:</strong></p>
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ tags:
<strong>Input:</strong> nums = [12,6,1,2,7]
<strong>Output:</strong> 77
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.
</pre>

<p><strong class="example">Example 2:</strong></p>
54 changes: 28 additions & 26 deletions solution/3400-3499/3481.Apply Substitutions/README.md
Original file line number Diff line number Diff line change
@@ -14,68 +14,70 @@ tags:

<!-- problem:start -->

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

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

## 题目描述

<!-- description:start -->

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

<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>
<p data-end="353" data-start="34">返回完全替换后 <strong>不</strong> 含任何 <strong>占位符</strong> 的&nbsp;<code>text</code>&nbsp;字符串。</p>

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

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

<div class="example-block">
<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>
<p><span class="example-io"><b>输入:</b>replacements = [["A","abc"],["B","def"]], text = "%A%_%B%"</span></p>

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

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

<ul data-end="238" data-start="71">
<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>
<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>
<li data-end="238" data-start="204">The final text becomes <code data-end="237" data-start="226">&quot;abc_def&quot;</code>.</li>
<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>
<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>
<li data-end="238" data-start="204">最终文本变为&nbsp;<code data-end="237" data-start="226">"abc_def"</code></li>
</ul>
</div>

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

<div class="example-block">
<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>
<p><span class="example-io"><b>输入:</b>replacements = [["A","bce"],["B","ace"],["C","abc%B%"]], text = "%A%_%B%_%C%"</span></p>

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

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

<ul data-end="541" data-is-last-node="" data-is-only-node="" data-start="255">
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<li data-end="541" data-is-last-node="" data-start="497">最终文本变为&nbsp;<code data-end="540" data-start="522">"bce_ace_abcace"</code></li>
</ul>
</div>

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

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

<ul>
<li data-end="1432" data-start="1398"><code>1 &lt;= replacements.length &lt;= 10</code></li>
<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:
<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>,其中:
<ul data-end="1683" data-start="1513">
<li data-end="1558" data-start="1513"><code data-end="1520" data-start="1515">key</code> is a single uppercase English letter.</li>
<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>
<li data-end="1558" data-start="1513"><code data-end="1520" data-start="1515">key</code>&nbsp;是一个大写英语字母。</li>
<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>
</ul>
</li>
<li data-end="726" data-start="688">All replacement keys are unique.</li>
<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>
<li data-end="726" data-start="688">所有的替换键互不相同。</li>
<li data-end="1875" data-start="1723"><code>text</code>&nbsp;字符串是通过从替换映射中随机串联所有 key 占位符(格式为 <code>%&lt;key&gt;%</code>)而形成的,以虚线分隔。</li>
<li data-end="1942" data-start="1876"><code>text.length == 4 * replacements.length - 1</code></li>
<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>
<li data-end="2265" data-start="2205">There are no cyclic dependencies between replacement keys.</li>
<li data-end="2052" data-start="1943"><code>text</code>&nbsp;或任何替换值中的每个占位符对应&nbsp;<code>replacements</code> 映射中的一个键。</li>
<li data-end="2265" data-start="2205">替换键之间没有循环依赖。</li>
</ul>

<!-- description:end -->
Loading