Skip to content

Commit aa26a08

Browse files
committed
✨feat: add 1758
1 parent 0079f9e commit aa26a08

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
| [1742. 盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/solution/by-ac_oier-3mxf/) | 简单 | 🤩🤩🤩 |
212212
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
213213
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
214+
| [1758. 生成交替二进制字符串的最少操作数](https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/solution/by-ac_oier-gclh/) | 简单 | 🤩🤩🤩🤩 |
214215
| [1763. 最长的美好子字符串](https://leetcode-cn.com/problems/longest-nice-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-nice-substring/solution/cong-shu-ju-fan-wei-xuan-ze-he-gua-suan-n3y2a/) | 简单 | 🤩🤩🤩 |
215216
| [1768. 交替合并字符串](https://leetcode.cn/problems/merge-strings-alternately/) | [LeetCode 题解链接](https://leetcode.cn/problems/merge-strings-alternately/solution/by-ac_oier-rjve/) | 简单 | 🤩🤩🤩 |
216217
| [1773. 统计匹配检索规则的物品数量](https://leetcode.cn/problems/count-items-matching-a-rule/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-items-matching-a-rule/solution/by-ac_oier-qyd6/) | 简单 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1758. 生成交替二进制字符串的最少操作数](https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/solution/by-ac_oier-gclh/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个仅由字符 `'0'``'1'` 组成的字符串 `s` 。一步操作中,你可以将任一 `'0'` 变成 `'1'` ,或者将 `'1'` 变成 `'0'`
10+
11+
交替字符串 定义为:如果字符串中不存在相邻两个字符相等的情况,那么该字符串就是交替字符串。例如,字符串 `"010"` 是交替字符串,而字符串 `"0100"` 不是。
12+
13+
返回使 `s` 变成 交替字符串 所需的 最少 操作数。
14+
15+
示例 1:
16+
```
17+
输入:s = "0100"
18+
19+
输出:1
20+
21+
解释:如果将最后一个字符变为 '1' ,s 就变成 "0101" ,即符合交替字符串定义。
22+
```
23+
示例 2:
24+
```
25+
输入:s = "10"
26+
27+
输出:0
28+
29+
解释:s 已经是交替字符串。
30+
```
31+
示例 3:
32+
```
33+
输入:s = "1111"
34+
35+
输出:2
36+
37+
解释:需要 2 步操作得到 "0101" 或 "1010" 。
38+
```
39+
40+
提示:
41+
* $1 <= s.length <= 10^4$
42+
* `s[i]``'0'``'1'`
43+
44+
---
45+
46+
### 模拟
47+
48+
最终结果只有「从 `0` 开始的交替串」和「从 `1` 开始的交替串」两种。
49+
50+
对于一个长度为 `n` 的未知序列 `A` 而言,假设我们需要花费 `cnt` 次操作将其变为「从 `0` 开始的交替串」,那么我们想要将其变为「从 `1` 开始的交替串」则需要 `n - cnt` 次操作:原本操作的 `cnt` 个位置不能动,而原本没操作的位置则都需要翻转,从而确保两种交替串对应位均相反。
51+
52+
Java 代码:
53+
```Java
54+
class Solution {
55+
public int minOperations(String s) {
56+
int n = s.length(), cnt = 0;
57+
for (int i = 0; i < n; i++) cnt += (s.charAt(i) - '0') ^ (i & 1);
58+
return Math.min(cnt, n - cnt);
59+
}
60+
}
61+
```
62+
TypeScript 代码:
63+
```TypeScript
64+
function minOperations(s: string): number {
65+
let n = s.length, cnt = 0
66+
for (let i = 0; i < n; i++) cnt += (s.charCodeAt(i) - '0'.charCodeAt(0)) ^ (i & 1)
67+
return Math.min(cnt, n - cnt)
68+
}
69+
```
70+
Python3 代码:
71+
```Python3
72+
class Solution:
73+
def minOperations(self, s: str) -> int:
74+
n, cnt = len(s), 0
75+
for i, c in enumerate(s):
76+
cnt += (ord(c) - ord('0')) ^ (i & 1)
77+
return min(cnt, n - cnt)
78+
```
79+
* 时间复杂度:$O(n)$
80+
* 空间复杂度:$O(1)$
81+
82+
---
83+
84+
### 最后
85+
86+
这是我们「刷穿 LeetCode」系列文章的第 `No.1758` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
87+
88+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
89+
90+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
91+
92+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
93+

0 commit comments

Comments
 (0)