Skip to content

feat: update lc problems #3653

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 4 commits into from
Oct 20, 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
2 changes: 1 addition & 1 deletion solution/0100-0199/0191.Number of 1 Bits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tags:

<!-- description:start -->

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tags:

<pre>
<b>输入:</b>nums = [3,2,6,1,4]
<b>输出:</b>2
<b>输出:1</b>
<b>解释:</b>我们执行以下操作:
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
由于下一次操作的分数与前一次不相等,我们无法继续进行任何操作。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3323.Mi

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

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

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

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

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README.md
---

<!-- problem:start -->

# [3324. 出现在屏幕上的字符串序列](https://leetcode.cn/problems/find-the-sequence-of-strings-appeared-on-the-screen)

[English Version](/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个字符串 <code>target</code>。</p>

<p>Alice 将会使用一种特殊的键盘在她的电脑上输入 <code>target</code>,这个键盘<strong> 只有两个 </strong>按键:</p>

<ul>
<li>按键 1:在屏幕上的字符串后追加字符 <code>'a'</code>。</li>
<li>按键 2:将屏幕上字符串的 <strong>最后一个 </strong>字符更改为英文字母表中的 <strong>下一个</strong> 字符。例如,<code>'c'</code> 变为 <code>'d'</code>,<code>'z'</code> 变为 <code>'a'</code>。</li>
</ul>

<p><strong>注意</strong>,最初屏幕上是一个<em>空</em>字符串 <code>""</code>,所以她<strong> 只能</strong> 按按键 1。</p>

<p>请你考虑按键次数 <strong>最少</strong> 的情况,按字符串出现顺序,返回 Alice 输入 <code>target</code> 时屏幕上出现的所有字符串列表。</p>

<p>&nbsp;</p>

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

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">target = "abc"</span></p>

<p><strong>输出:</strong> <span class="example-io">["a","aa","ab","aba","abb","abc"]</span></p>

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

<p>Alice 按键的顺序如下:</p>

<ul>
<li>按下按键 1,屏幕上的字符串变为 <code>"a"</code>。</li>
<li>按下按键 1,屏幕上的字符串变为 <code>"aa"</code>。</li>
<li>按下按键 2,屏幕上的字符串变为 <code>"ab"</code>。</li>
<li>按下按键 1,屏幕上的字符串变为 <code>"aba"</code>。</li>
<li>按下按键 2,屏幕上的字符串变为 <code>"abb"</code>。</li>
<li>按下按键 2,屏幕上的字符串变为 <code>"abc"</code>。</li>
</ul>
</div>

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

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">target = "he"</span></p>

<p><strong>输出:</strong> <span class="example-io">["a","b","c","d","e","f","g","h","ha","hb","hc","hd","he"]</span></p>
</div>

<p>&nbsp;</p>

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

<ul>
<li><code>1 &lt;= target.length &lt;= 400</code></li>
<li><code>target</code> 仅由小写英文字母组成。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们可以模拟 Alice 按键的过程,从空字符串开始,每次按键后更新字符串,直到得到目标字符串。

时间复杂度 $O(n^2 \times |\Sigma|)$,其中 $n$ 是目标字符串的长度,而 $\Sigma$ 是字符集,这里是小写字母集合,因此 $|\Sigma| = 26$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def stringSequence(self, target: str) -> List[str]:
ans = []
for c in target:
s = ans[-1] if ans else ""
for a in ascii_lowercase:
t = s + a
ans.append(t)
if a == c:
break
return ans
```

#### Java

```java
class Solution {
public List<String> stringSequence(String target) {
List<String> ans = new ArrayList<>();
for (char c : target.toCharArray()) {
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
for (char a = 'a'; a <= c; ++a) {
String t = s + a;
ans.add(t);
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<string> stringSequence(string target) {
vector<string> ans;
for (char c : target) {
string s = ans.empty() ? "" : ans.back();
for (char a = 'a'; a <= c; ++a) {
string t = s + a;
ans.push_back(t);
}
}
return ans;
}
};
```

#### Go

```go
func stringSequence(target string) (ans []string) {
for _, c := range target {
s := ""
if len(ans) > 0 {
s = ans[len(ans)-1]
}
for a := 'a'; a <= c; a++ {
t := s + string(a)
ans = append(ans, t)
}
}
return
}
```

#### TypeScript

```ts
function stringSequence(target: string): string[] {
const ans: string[] = [];
for (const c of target) {
let s = ans.length > 0 ? ans[ans.length - 1] : '';
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
const t = s + String.fromCharCode(a);
ans.push(t);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
---
comments: true
difficulty: Medium
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
---

<!-- problem:start -->

# [3324. Find the Sequence of Strings Appeared on the Screen](https://leetcode.com/problems/find-the-sequence-of-strings-appeared-on-the-screen)

[中文文档](/solution/3300-3399/3324.Find%20the%20Sequence%20of%20Strings%20Appeared%20on%20the%20Screen/README.md)

## Description

<!-- description:start -->

<p>You are given a string <code>target</code>.</p>

<p>Alice is going to type <code>target</code> on her computer using a special keyboard that has <strong>only two</strong> keys:</p>

<ul>
<li>Key 1 appends the character <code>&quot;a&quot;</code> to the string on the screen.</li>
<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>
</ul>

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

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

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">target = &quot;abc&quot;</span></p>

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

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

<p>The sequence of key presses done by Alice are:</p>

<ul>
<li>Press key 1, and the string on the screen becomes <code>&quot;a&quot;</code>.</li>
<li>Press key 1, and the string on the screen becomes <code>&quot;aa&quot;</code>.</li>
<li>Press key 2, and the string on the screen becomes <code>&quot;ab&quot;</code>.</li>
<li>Press key 1, and the string on the screen becomes <code>&quot;aba&quot;</code>.</li>
<li>Press key 2, and the string on the screen becomes <code>&quot;abb&quot;</code>.</li>
<li>Press key 2, and the string on the screen becomes <code>&quot;abc&quot;</code>.</li>
</ul>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">target = &quot;he&quot;</span></p>

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

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

<ul>
<li><code>1 &lt;= target.length &lt;= 400</code></li>
<li><code>target</code> consists only of lowercase English letters.</li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Simulation

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.

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

<!-- tabs:start -->

#### Python3

```python
class Solution:
def stringSequence(self, target: str) -> List[str]:
ans = []
for c in target:
s = ans[-1] if ans else ""
for a in ascii_lowercase:
t = s + a
ans.append(t)
if a == c:
break
return ans
```

#### Java

```java
class Solution {
public List<String> stringSequence(String target) {
List<String> ans = new ArrayList<>();
for (char c : target.toCharArray()) {
String s = ans.isEmpty() ? "" : ans.get(ans.size() - 1);
for (char a = 'a'; a <= c; ++a) {
String t = s + a;
ans.add(t);
}
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<string> stringSequence(string target) {
vector<string> ans;
for (char c : target) {
string s = ans.empty() ? "" : ans.back();
for (char a = 'a'; a <= c; ++a) {
string t = s + a;
ans.push_back(t);
}
}
return ans;
}
};
```

#### Go

```go
func stringSequence(target string) (ans []string) {
for _, c := range target {
s := ""
if len(ans) > 0 {
s = ans[len(ans)-1]
}
for a := 'a'; a <= c; a++ {
t := s + string(a)
ans = append(ans, t)
}
}
return
}
```

#### TypeScript

```ts
function stringSequence(target: string): string[] {
const ans: string[] = [];
for (const c of target) {
let s = ans.length > 0 ? ans[ans.length - 1] : '';
for (let a = 'a'.charCodeAt(0); a <= c.charCodeAt(0); a++) {
const t = s + String.fromCharCode(a);
ans.push(t);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading