Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78af72d

Browse files
✨feat: Add 1576
1 parent 74c17f5 commit 78af72d

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
8282
| [1486. 数组异或操作](https://leetcode-cn.com/problems/xor-operation-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-operation-in-an-array/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-dggg/) | 简单 | 🤩🤩🤩 |
8383
| [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 |
84+
| [1576. 替换所有的问号](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-fa1u/) | 简单 | 🤩🤩🤩🤩🤩 |
8485
| [1583. 统计不开心的朋友](https://leetcode-cn.com/problems/count-unhappy-friends/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-unhappy-friends/solution/gong-shui-san-xie-ha-xi-biao-mo-ni-ti-by-2qy0/) | 中等 | 🤩🤩🤩🤩 |
8586
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
8687
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1576. 替换所有的问号](https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-fa1u/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个仅包含小写英文字母和 `'?'` 字符的字符串 `s`,请你将所有的 `'?'` 转换为若干小写字母,使最终的字符串不包含任何 **连续重复** 的字符。
10+
11+
注意:你 不能 修改非 `'?'` 字符。
12+
13+
题目测试用例保证 除 `'?'` 字符 之外,不存在连续重复的字符。
14+
15+
在完成所有转换(可能无需转换)后返回最终的字符串。如果有多个解决方案,请返回其中任何一个。可以证明,在给定的约束条件下,答案总是存在的。
16+
17+
示例 1:
18+
```
19+
输入:s = "?zs"
20+
21+
输出:"azs"
22+
23+
解释:该示例共有 25 种解决方案,从 "azs" 到 "yzs" 都是符合题目要求的。只有 "z" 是无效的修改,因为字符串 "zzs" 中有连续重复的两个 'z' 。
24+
```
25+
示例 2:
26+
```
27+
输入:s = "ubv?w"
28+
29+
输出:"ubvaw"
30+
31+
解释:该示例共有 24 种解决方案,只有替换成 "v" 和 "w" 不符合题目要求。因为 "ubvvw" 和 "ubvww" 都包含连续重复的字符。
32+
```
33+
示例 3:
34+
```
35+
输入:s = "j?qg??b"
36+
37+
输出:"jaqgacb"
38+
```
39+
示例 4:
40+
```
41+
输入:s = "??yw?ipkj?"
42+
43+
输出:"acywaipkja"
44+
```
45+
46+
提示:
47+
* 1ドル <= s.length <= 100$
48+
* `s` 仅包含小写英文字母和 `'?'` 字符
49+
50+
51+
---
52+
53+
### 模拟
54+
55+
根据题意进行模拟,尝试对每个 $s[i]$ 进行替换,能够替换的前提是 $s[i]$ 为 `?`,且替换字符与前后字符(若存在)不同,由于只需要确保与前后字符不同,因此必然最多在 3ドル$ 个字符内找到可替换的值。
56+
57+
代码:
58+
```Java
59+
class Solution {
60+
public String modifyString(String s) {
61+
char[] cs = s.toCharArray();
62+
int n = cs.length;
63+
for (int i = 0; i < n; i++) {
64+
for (int c = 0; c < 3 && cs[i] == '?'; c++) {
65+
boolean ok = true;
66+
if (i - 1 >= 0 && cs[i - 1] == c + 'a') ok = false;
67+
if (i + 1 < n && cs[i + 1] == c + 'a') ok = false;
68+
if (ok) cs[i] = (char)(c + 'a');
69+
}
70+
}
71+
return String.valueOf(cs);
72+
}
73+
}
74+
```
75+
* 时间复杂度:会处理 `s` 中的所有字符,若当前字符为 `?` 时,会检查与前后字符的关系,最多会遍历在 3ドル$ 个字符内找到可替换的字符,复杂度为 $O(n * C),ドル其中 $C = 3$
76+
* 空间复杂度:$O(n)$
77+
78+
---
79+
80+
### 最后
81+
82+
这是我们「刷穿 LeetCode」系列文章的第 `No.1576` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
83+
84+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
85+
86+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
87+
88+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
89+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /