Skip to content

Commit a377858

Browse files
authored
feat: update lc problems (#3653)
1 parent 87f3911 commit a377858

File tree

23 files changed

+1099
-5
lines changed

23 files changed

+1099
-5
lines changed

solution/0100-0199/0191.Number of 1 Bits/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tags:
1717

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

20-
<p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
20+
<p>Given a positive integer <code>n</code>, write a function that returns the number of <span data-keyword="set-bit">set bits</span> in its binary representation (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
2121

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

solution/3000-3099/3038.Maximum Number of Operations With the Same Score I/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tags:
4747

4848
<pre>
4949
<b>输入:</b>nums = [3,2,6,1,4]
50-
<b>输出:</b>2
50+
<b>输出:1</b>
5151
<b>解释:</b>我们执行以下操作:
5252
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
5353
由于下一次操作的分数与前一次不相等,我们无法继续进行任何操作。

solution/3300-3399/3323.Minimize Connected Groups by Inserting Interval/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3323.Mi
1616

1717
<p>给定一个 2 维数组&nbsp;<code>intervals</code>,其中&nbsp;<code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>&nbsp;表示区间&nbsp;<code>i</code>&nbsp;的开头和结尾。另外还给定一个整数&nbsp;<code>k</code>。</p>
1818

19-
<p>你必须向数组添加 <strong>恰好一个</strong>&nbsp;新的区间&nbsp;<code>[start<sub>new</sub>, end<sub>new</sub>]</code>&nbsp;使得:</p>
19+
<p>你必须向数组 <strong>恰好添加一个</strong>&nbsp;新的区间&nbsp;<code>[start<sub>new</sub>, end<sub>new</sub>]</code>&nbsp;使得:</p>
2020

2121
<ul>
2222
<li>新区间的长度,<code>end<sub>new</sub> - start<sub>new</sub></code>&nbsp;最多为&nbsp;<code>k</code>。</li>
@@ -30,7 +30,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3323.Mi
3030
<li>然而,区间组&nbsp;<code>[[1, 2], [3, 4]]</code>&nbsp;不是连通的,因为&nbsp;<code>(2, 3)</code>&nbsp;段没有被覆盖。</li>
3131
</ul>
3232

33-
<p>返回在数组添加 <strong>恰好一个</strong> 新区间后,连通组的 <strong>最小</strong> 数量。</p>
33+
<p>返回在数组&nbsp;<strong>恰好添加一个</strong> 新区间后,连通组的 <strong>最小</strong> 数量。</p>
3434

3535
<p>&nbsp;</p>
3636

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3324. 出现在屏幕上的字符串序列](https://leetcode.cn/problems/find-the-sequence-of-strings-appeared-on-the-screen)
10+
11+
[English Version](/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个字符串 <code>target</code>。</p>
18+
19+
<p>Alice 将会使用一种特殊的键盘在她的电脑上输入 <code>target</code>,这个键盘<strong> 只有两个 </strong>按键:</p>
20+
21+
<ul>
22+
<li>按键 1:在屏幕上的字符串后追加字符 <code>'a'</code>。</li>
23+
<li>按键 2:将屏幕上字符串的 <strong>最后一个 </strong>字符更改为英文字母表中的 <strong>下一个</strong> 字符。例如,<code>'c'</code> 变为 <code>'d'</code>,<code>'z'</code> 变为 <code>'a'</code>。</li>
24+
</ul>
25+
26+
<p><strong>注意</strong>,最初屏幕上是一个<em>空</em>字符串 <code>""</code>,所以她<strong> 只能</strong> 按按键 1。</p>
27+
28+
<p>请你考虑按键次数 <strong>最少</strong> 的情况,按字符串出现顺序,返回 Alice 输入 <code>target</code> 时屏幕上出现的所有字符串列表。</p>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong class="example">示例 1:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>输入:</strong> <span class="example-io">target = "abc"</span></p>
36+
37+
<p><strong>输出:</strong> <span class="example-io">["a","aa","ab","aba","abb","abc"]</span></p>
38+
39+
<p><strong>解释:</strong></p>
40+
41+
<p>Alice 按键的顺序如下:</p>
42+
43+
<ul>
44+
<li>按下按键 1,屏幕上的字符串变为 <code>"a"</code>。</li>
45+
<li>按下按键 1,屏幕上的字符串变为 <code>"aa"</code>。</li>
46+
<li>按下按键 2,屏幕上的字符串变为 <code>"ab"</code>。</li>
47+
<li>按下按键 1,屏幕上的字符串变为 <code>"aba"</code>。</li>
48+
<li>按下按键 2,屏幕上的字符串变为 <code>"abb"</code>。</li>
49+
<li>按下按键 2,屏幕上的字符串变为 <code>"abc"</code>。</li>
50+
</ul>
51+
</div>
52+
53+
<p><strong class="example">示例 2:</strong></p>
54+
55+
<div class="example-block">
56+
<p><strong>输入:</strong> <span class="example-io">target = "he"</span></p>
57+
58+
<p><strong>输出:</strong> <span class="example-io">["a","b","c","d","e","f","g","h","ha","hb","hc","hd","he"]</span></p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= target.length &lt;= 400</code></li>
67+
<li><code>target</code> 仅由小写英文字母组成。</li>
68+
</ul>
69+
70+
<!-- description:end -->
71+
72+
## 解法
73+
74+
<!-- solution:start -->
75+
76+
### 方法一:模拟
77+
78+
我们可以模拟 Alice 按键的过程,从空字符串开始,每次按键后更新字符串,直到得到目标字符串。
79+
80+
时间复杂度 $O(n^2 \times |\Sigma|)$,其中 $n$ 是目标字符串的长度,而 $\Sigma$ 是字符集,这里是小写字母集合,因此 $|\Sigma| = 26$。
81+
82+
<!-- tabs:start -->
83+
84+
#### Python3
85+
86+
```python
87+
class Solution:
88+
def stringSequence(self, target: str) -> List[str]:
89+
ans = []
90+
for c in target:
91+
s = ans[-1] if ans else ""
92+
for a in ascii_lowercase:
93+
t = s + a
94+
ans.append(t)
95+
if a == c:
96+
break
97+
return ans
98+
```
99+
100+
#### Java
101+
102+
```java
103+
class Solution {
104+
public List<String> stringSequence(String target) {
105+
List<String> ans = new ArrayList<>();
106+
for (char c : target.toCharArray()) {
107+
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
108+
for (char a = 'a'; a <= c; ++a) {
109+
String t = s + a;
110+
ans.add(t);
111+
}
112+
}
113+
return ans;
114+
}
115+
}
116+
```
117+
118+
#### C++
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<string> stringSequence(string target) {
124+
vector<string> ans;
125+
for (char c : target) {
126+
string s = ans.empty() ? "" : ans.back();
127+
for (char a = 'a'; a <= c; ++a) {
128+
string t = s + a;
129+
ans.push_back(t);
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
#### Go
138+
139+
```go
140+
func stringSequence(target string) (ans []string) {
141+
for _, c := range target {
142+
s := ""
143+
if len(ans) > 0 {
144+
s = ans[len(ans)-1]
145+
}
146+
for a := 'a'; a <= c; a++ {
147+
t := s + string(a)
148+
ans = append(ans, t)
149+
}
150+
}
151+
return
152+
}
153+
```
154+
155+
#### TypeScript
156+
157+
```ts
158+
function stringSequence(target: string): string[] {
159+
const ans: string[] = [];
160+
for (const c of target) {
161+
let s = ans.length > 0 ? ans[ans.length - 1] : '';
162+
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
163+
const t = s + String.fromCharCode(a);
164+
ans.push(t);
165+
}
166+
}
167+
return ans;
168+
}
169+
```
170+
171+
<!-- tabs:end -->
172+
173+
<!-- solution:end -->
174+
175+
<!-- problem:end -->
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3324. Find the Sequence of Strings Appeared on the Screen](https://leetcode.com/problems/find-the-sequence-of-strings-appeared-on-the-screen)
10+
11+
[中文文档](/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given a string <code>target</code>.</p>
18+
19+
<p>Alice is going to type <code>target</code> on her computer using a special keyboard that has <strong>only two</strong> keys:</p>
20+
21+
<ul>
22+
<li>Key 1 appends the character <code>&quot;a&quot;</code> to the string on the screen.</li>
23+
<li>Key 2 changes the <strong>last</strong> character of the string on the screen to its <strong>next</strong> character in the English alphabet. For example, <code>&quot;c&quot;</code> changes to <code>&quot;d&quot;</code> and <code>&quot;z&quot;</code> changes to <code>&quot;a&quot;</code>.</li>
24+
</ul>
25+
26+
<p><strong>Note</strong> that initially there is an <em>empty</em> string <code>&quot;&quot;</code> on the screen, so she can <strong>only</strong> press key 1.</p>
27+
28+
<p>Return a list of <em>all</em> strings that appear on the screen as Alice types <code>target</code>, in the order they appear, using the <strong>minimum</strong> key presses.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong class="example">Example 1:</strong></p>
32+
33+
<div class="example-block">
34+
<p><strong>Input:</strong> <span class="example-io">target = &quot;abc&quot;</span></p>
35+
36+
<p><strong>Output:</strong> <span class="example-io">[&quot;a&quot;,&quot;aa&quot;,&quot;ab&quot;,&quot;aba&quot;,&quot;abb&quot;,&quot;abc&quot;]</span></p>
37+
38+
<p><strong>Explanation:</strong></p>
39+
40+
<p>The sequence of key presses done by Alice are:</p>
41+
42+
<ul>
43+
<li>Press key 1, and the string on the screen becomes <code>&quot;a&quot;</code>.</li>
44+
<li>Press key 1, and the string on the screen becomes <code>&quot;aa&quot;</code>.</li>
45+
<li>Press key 2, and the string on the screen becomes <code>&quot;ab&quot;</code>.</li>
46+
<li>Press key 1, and the string on the screen becomes <code>&quot;aba&quot;</code>.</li>
47+
<li>Press key 2, and the string on the screen becomes <code>&quot;abb&quot;</code>.</li>
48+
<li>Press key 2, and the string on the screen becomes <code>&quot;abc&quot;</code>.</li>
49+
</ul>
50+
</div>
51+
52+
<p><strong class="example">Example 2:</strong></p>
53+
54+
<div class="example-block">
55+
<p><strong>Input:</strong> <span class="example-io">target = &quot;he&quot;</span></p>
56+
57+
<p><strong>Output:</strong> <span class="example-io">[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;ha&quot;,&quot;hb&quot;,&quot;hc&quot;,&quot;hd&quot;,&quot;he&quot;]</span></p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= target.length &lt;= 400</code></li>
65+
<li><code>target</code> consists only of lowercase English letters.</li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## Solutions
71+
72+
<!-- solution:start -->
73+
74+
### Solution 1: Simulation
75+
76+
We can simulate Alice's typing process, starting from an empty string and updating the string after each keystroke until the target string is obtained.
77+
78+
The time complexity is $O(n^2 \times |\Sigma|)$, where $n$ is the length of the target string and $\Sigma$ is the character set, which in this case is the set of lowercase letters, so $|\Sigma| = 26$.
79+
80+
<!-- tabs:start -->
81+
82+
#### Python3
83+
84+
```python
85+
class Solution:
86+
def stringSequence(self, target: str) -> List[str]:
87+
ans = []
88+
for c in target:
89+
s = ans[-1] if ans else ""
90+
for a in ascii_lowercase:
91+
t = s + a
92+
ans.append(t)
93+
if a == c:
94+
break
95+
return ans
96+
```
97+
98+
#### Java
99+
100+
```java
101+
class Solution {
102+
public List<String> stringSequence(String target) {
103+
List<String> ans = new ArrayList<>();
104+
for (char c : target.toCharArray()) {
105+
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
106+
for (char a = 'a'; a <= c; ++a) {
107+
String t = s + a;
108+
ans.add(t);
109+
}
110+
}
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
#### C++
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
vector<string> stringSequence(string target) {
122+
vector<string> ans;
123+
for (char c : target) {
124+
string s = ans.empty() ? "" : ans.back();
125+
for (char a = 'a'; a <= c; ++a) {
126+
string t = s + a;
127+
ans.push_back(t);
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
134+
135+
#### Go
136+
137+
```go
138+
func stringSequence(target string) (ans []string) {
139+
for _, c := range target {
140+
s := ""
141+
if len(ans) > 0 {
142+
s = ans[len(ans)-1]
143+
}
144+
for a := 'a'; a <= c; a++ {
145+
t := s + string(a)
146+
ans = append(ans, t)
147+
}
148+
}
149+
return
150+
}
151+
```
152+
153+
#### TypeScript
154+
155+
```ts
156+
function stringSequence(target: string): string[] {
157+
const ans: string[] = [];
158+
for (const c of target) {
159+
let s = ans.length > 0 ? ans[ans.length - 1] : '';
160+
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
161+
const t = s + String.fromCharCode(a);
162+
ans.push(t);
163+
}
164+
}
165+
return ans;
166+
}
167+
```
168+
169+
<!-- tabs:end -->
170+
171+
<!-- solution:end -->
172+
173+
<!-- problem:end -->

0 commit comments

Comments
 (0)