Skip to content

Commit 61d7c48

Browse files
committed
Add solution 1576
1 parent 9e302d2 commit 61d7c48

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

leetcode/0390.Elimination-Game/390. Elimination Game.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package leetcode
22

33
func lastRemaining(n int) int {
4-
start, dir, cnt, step := 1, true, n, 1
5-
for cnt > 1 {
4+
start, dir, step := 1, true, 1
5+
for n > 1 {
66
if dir { // 正向
77
start += step
88
} else { // 反向
9-
if cnt%2 == 1 {
9+
if n%2 == 1 {
1010
start += step
1111
}
1212
}
1313
dir = !dir
14-
cnt >>= 1
14+
n >>= 1
1515
step <<= 1
1616
}
1717
return start
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func modifyString(s string) string {
4+
res := []byte(s)
5+
for i, ch := range res {
6+
if ch == '?' {
7+
for b := byte('a'); b <= 'z'; b++ {
8+
if !(i > 0 && res[i-1] == b || i < len(res)-1 && res[i+1] == b) {
9+
res[i] = b
10+
break
11+
}
12+
}
13+
}
14+
}
15+
return string(res)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1576 struct {
9+
para1576
10+
ans1576
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1576 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1576 struct {
22+
one string
23+
}
24+
25+
func Test_Problem1576(t *testing.T) {
26+
27+
qs := []question1576{
28+
29+
{
30+
para1576{"?zs"},
31+
ans1576{"azs"},
32+
},
33+
34+
{
35+
para1576{"ubv?w"},
36+
ans1576{"ubvaw"},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1576------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1576, q.para1576
44+
fmt.Printf("【input】:%v 【output】:%v \n", p, modifyString(p.s))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [1576. Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)
2+
3+
## 题目
4+
5+
Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
6+
7+
It is **guaranteed** that there are no consecutive repeating characters in the given string **except** for `'?'`.
8+
9+
Return *the final string after all the conversions (possibly zero) have been made*. If there is more than one solution, return **any of them**. It can be shown that an answer is always possible with the given constraints.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: s = "?zs"
15+
Output: "azs"
16+
Explanation: There are 25 solutions for this problem. From "azs" to "yzs", all are valid. Only "z" is an invalid modification as the string will consist of consecutive repeating characters in "zzs".
17+
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: s = "ubv?w"
24+
Output: "ubvaw"
25+
Explanation: There are 24 solutions for this problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in "ubvvw" and "ubvww".
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `1 <= s.length <= 100`
32+
- `s` consist of lowercase English letters and `'?'`.
33+
34+
## 题目大意
35+
36+
给你一个仅包含小写英文字母和 '?' 字符的字符串 s,请你将所有的 '?' 转换为若干小写字母,使最终的字符串不包含任何 连续重复 的字符。注意:你 不能 修改非 '?' 字符。
37+
38+
题目测试用例保证 除 '?' 字符 之外,不存在连续重复的字符。在完成所有转换(可能无需转换)后返回最终的字符串。如果有多个解决方案,请返回其中任何一个。可以证明,在给定的约束条件下,答案总是存在的。
39+
40+
## 解题思路
41+
42+
- 简单题。找到源字符串中 ‘?’ 字符的位置,然后依次用 a ~ z 字符去替换,替换进去的字符不能和前后字符相同即可。
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
func modifyString(s string) string {
50+
res := []byte(s)
51+
for i, ch := range res {
52+
if ch == '?' {
53+
for b := byte('a'); b <= 'z'; b++ {
54+
if !(i > 0 && res[i-1] == b || i < len(res)-1 && res[i+1] == b) {
55+
res[i] = b
56+
break
57+
}
58+
}
59+
}
60+
}
61+
return string(res)
62+
}
63+
```

0 commit comments

Comments
 (0)