Skip to content

Commit dd8e431

Browse files
authored
update: 添加问题“1888.使二进制字符串字符交替的最少反转次数”的代码和题解 (#1434)
* 1888: WA.cpp (#1431) * 1888: why.cpp (#1431) * 1888: WA.cpp (#1431) input: 001000000010 should: 4 my: 3 * 1888: why.cpp (#1431) input: 001000000010 should: 4 my: 3 * 1888: WA.cpp (#1431) input: 01001001101 should: 2 my: 5 * 1888: AC.cpp (#1431) * update: 添加问题“1888.使二进制字符串字符交替的最少反转次数”的代码和题解 (#1434) Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent be3b2c9 commit dd8e431

7 files changed

Lines changed: 249 additions & 12 deletions

.commitmsg

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-07 17:46:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-08 11:41:56
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
12+
/*
13+
01001001101
14+
3次右移
15+
01001101010
16+
||
17+
01010101010
18+
19+
相当于
20+
01001001101
21+
||
22+
01001010101
23+
--
24+
01010101010
25+
_ _
26+
*/
27+
class Solution {
28+
public:
29+
int minFlips(const string& s) {
30+
int total = 0, n = s.size();
31+
for (int i = 0; i < n; i++) {
32+
total += s[i] % 2 == i % 2;
33+
}
34+
int ans = min(total, n - total);
35+
if (n % 2 == 0) {
36+
return ans;
37+
}
38+
for (int i = 0, now = 0; i < n; i++) {
39+
now += s[i] % 2 == i % 2;
40+
int original_front = now;
41+
int original_back = total - original_front;
42+
int changed_front = i + 1 - original_front;
43+
int changed_back = n - i - 1 - original_back;
44+
ans = min(ans, min(original_front + changed_back, changed_front + original_back));
45+
}
46+
return ans;
47+
}
48+
};
49+
50+
#if defined(_WIN32) || defined(__APPLE__)
51+
/*
52+
001000000010
53+
| | | |
54+
101010101010
55+
56+
001000000010
57+
| | |
58+
001010101010
59+
- 右移一位
60+
010101010100
61+
3次不可
62+
63+
4
64+
*/
65+
int main() {
66+
string s;
67+
while (cin >> s) {
68+
Solution sol;
69+
cout << sol.minFlips(s) << endl;
70+
}
71+
return 0;
72+
}
73+
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@
696696
|1870.准时到达的列车最小时速|中等|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/02/LeetCode%201870.%E5%87%86%E6%97%B6%E5%88%B0%E8%BE%BE%E7%9A%84%E5%88%97%E8%BD%A6%E6%9C%80%E5%B0%8F%E6%97%B6%E9%80%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142680612" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/solutions/2937033/letmefly-1870zhun-shi-dao-da-de-lie-che-liv8g/" target="_blank">LeetCode题解</a>|
697697
|1877.数组中最大数对和的最小值|中等|<a href="https://leetcode.cn/problems/minimize-maximum-pair-sum-in-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/24/LeetCode%201877.%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9C%80%E5%A4%A7%E6%95%B0%E5%AF%B9%E5%92%8C%E7%9A%84%E6%9C%80%E5%B0%8F%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157333871" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimize-maximum-pair-sum-in-array/solutions/3888405/letmefly-1877shu-zu-zhong-zui-da-shu-dui-sm70/" target="_blank">LeetCode题解</a>|
698698
|1884.鸡蛋掉落-两枚鸡蛋|中等|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/13/LeetCode%201884.%E9%B8%A1%E8%9B%8B%E6%8E%89%E8%90%BD-%E4%B8%A4%E6%9E%9A%E9%B8%A1%E8%9B%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142906976" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/solutions/2949710/letmefly-1884ji-dan-diao-luo-liang-mei-j-saz6/" target="_blank">LeetCode题解</a>|
699+
|1888.使二进制字符串字符交替的最少反转次数|中等|<a href="https://leetcode.cn/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/07/LeetCode%201888.%E4%BD%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AD%97%E7%AC%A6%E4%BA%A4%E6%9B%BF%E7%9A%84%E6%9C%80%E5%B0%91%E5%8F%8D%E8%BD%AC%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158813763" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/solutions/3918582/letmefly-1888shi-er-jin-zhi-zi-fu-chuan-8b104/" target="_blank">LeetCode题解</a>|
699700
|1895.最大的幻方|中等|<a href="https://leetcode.cn/problems/largest-magic-square/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/18/LeetCode%201895.%E6%9C%80%E5%A4%A7%E7%9A%84%E5%B9%BB%E6%96%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/157104818" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-magic-square/solutions/3883805/letmefly-1895zui-da-de-huan-fang-bao-li-9ln7k/" target="_blank">LeetCode题解</a>|
700701
|1901.寻找峰值II|中等|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/19/LeetCode%201901.%E5%AF%BB%E6%89%BE%E5%B3%B0%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135083347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/solutions/2572012/letmefly-1901xun-zhao-feng-zhi-iier-fen-19tmj/" target="_blank">LeetCode题解</a>|
701702
|1911.最大子序列交替和|中等|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/11/LeetCode%201911.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%88%97%E4%BA%A4%E6%9B%BF%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131652316" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/solutions/2339029/letmefly-1911zui-da-zi-xu-lie-jiao-ti-he-fyzq/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 1758.生成交替二进制字符串的最少操作数.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,6 @@ public:
115115
};
116116
```
117117

118-
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/11/29/LeetCode%201758.%E7%94%9F%E6%88%90%E4%BA%A4%E6%9B%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%95%B0/)哦~
119-
> Tisfy:[https://letmefly.blog.csdn.net/article/details/128107132](https://letmefly.blog.csdn.net/article/details/128107132)
118+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/128107132)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2022/11/29/LeetCode%201758.%E7%94%9F%E6%88%90%E4%BA%A4%E6%9B%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%95%B0/)哦~
119+
>
120+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/LeetCode 1784.检查二进制字符串字段.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,6 @@ impl Solution {
193193
}
194194
```
195195

196-
197-
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/10/03/LeetCode%201784.%E6%A3%80%E6%9F%A5%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AD%97%E6%AE%B5/)哦~
198-
> Tisfy:[https://letmefly.blog.csdn.net/article/details/127150307](https://letmefly.blog.csdn.net/article/details/127150307)
196+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/127150307)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2022/10/03/LeetCode%201784.%E6%A3%80%E6%9F%A5%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AD%97%E6%AE%B5/)哦~
197+
>
198+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
title: 1888.使二进制字符串字符交替的最少反转次数:前缀和O(1)
3+
date: 2026-03-08 15:09:24
4+
tags: [题解, LeetCode, 中等, 字符串, 动态规划, 滑动窗口]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1888.使二进制字符串字符交替的最少反转次数:前缀和O(1)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/](https://leetcode.cn/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/)
11+
12+
<p>给你一个二进制字符串 <code>s</code> 。你可以按任意顺序执行以下两种操作任意次:</p>
13+
14+
<ul>
15+
<li><strong>类型 1 :删除</strong> 字符串 <code>s</code> 的第一个字符并将它 <strong>添加</strong> 到字符串结尾。</li>
16+
<li><strong>类型 2 :选择 </strong>字符串 <code>s</code> 中任意一个字符并将该字符 <strong>反转 </strong>,也就是如果值为 <code>'0'</code> ,则反转得到 <code>'1'</code> ,反之亦然。</li>
17+
</ul>
18+
19+
<p>请你返回使 <code>s</code> 变成 <strong>交替</strong> 字符串的前提下, <strong>类型 2 </strong>的 <strong>最少</strong> 操作次数 。</p>
20+
21+
<p>我们称一个字符串是 <strong>交替</strong> 的,需要满足任意相邻字符都不同。</p>
22+
23+
<ul>
24+
<li>比方说,字符串 <code>"010"</code> 和 <code>"1010"</code> 都是交替的,但是字符串 <code>"0100"</code> 不是。</li>
25+
</ul>
26+
27+
<p> </p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre><b>输入:</b>s = "111000"
32+
<b>输出:</b>2
33+
<b>解释:</b>执行第一种操作两次,得到 s = "100011" 。
34+
然后对第三个和第六个字符执行第二种操作,得到 s = "10<strong>1</strong>01<strong>0</strong>" 。
35+
</pre>
36+
37+
<p><strong>示例 2:</strong></p>
38+
39+
<pre><b>输入:</b>s = "010"
40+
<b>输出:</b>0
41+
<strong>解释:</strong>字符串已经是交替的。
42+
</pre>
43+
44+
<p><strong>示例 3:</strong></p>
45+
46+
<pre><b>输入:</b>s = "1110"
47+
<b>输出:</b>1
48+
<b>解释:</b>对第二个字符执行第二种操作,得到 s = "1<strong>0</strong>10" 。
49+
</pre>
50+
51+
<p> </p>
52+
53+
<p><strong>提示:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
57+
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
58+
</ul>
59+
60+
61+
62+
## 解题方法:前缀和
63+
64+
如果没有操作1(字符串轮转),那么问题就变成了[1758. 生成交替二进制字符串的最少操作数](https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/)
65+
66+
现在多了个操作1,有什么用呢?
67+
68+
> 例如`110`,把`1`往后轮转,就变成了`101`,就不用翻转操作了。
69+
70+
其实不难发现轮转操作只对字符串长度为奇数时候有效:
71+
72+
> 如果字符串长度为偶数的话,最终有效的`1010`要么由`1010`变来,要么由`0101`变来。总之就是最终的01交替字符串无论是经过怎样的轮转得来的,其轮转之前就一定是01交替字符串。
73+
>
74+
> 但是奇数长度的字符串就不一样了,奇数长度的01字符串首位字符相同,所以最终有效的01字符串首位字符一定相同(如`10101`),所以就可能是原有两个相邻相同元素拆开轮转得到的(如`01101`
75+
76+
所以我们模拟这个“拆开的位置”就好了。对于奇数长度的字符串,先假设变成0开头0结尾:
77+
78+
具体来说,`01101`如果全部变成`01010`需要改变3次,从前到后遍历字符串,遍历过`01`后,若从此处拆开,相当于后面本来要变成`010`的三个字符要变成`101`,后面只需要变化`len(101) - 3 = 0`次就好了。
79+
80+
记下全部变成`010`需要反转的次数,然后从前到后遍历,记录下遍历到的位置为止变成`010..`需要反转的次数,那么后面变成`101...`需要的次数就是`len(后面字符串长度) - (原本总反转次数 - 前面已经反转次数)`
81+
82+
讨论过变成010后,变成101同理。
83+
84+
+ 时间复杂度$O(len(s))$
85+
+ 空间复杂度$O(1)$
86+
87+
### AC代码
88+
89+
#### C++
90+
91+
```cpp
92+
/*
93+
* @LastEditTime: 2026-03-08 11:41:56
94+
*/
95+
/*
96+
01001001101
97+
3次右移
98+
01001101010
99+
||
100+
01010101010
101+
102+
相当于
103+
01001001101
104+
||
105+
01001010101
106+
--
107+
01010101010
108+
_ _
109+
*/
110+
class Solution {
111+
public:
112+
int minFlips(const string& s) {
113+
int total = 0, n = s.size();
114+
for (int i = 0; i < n; i++) {
115+
total += s[i] % 2 == i % 2;
116+
}
117+
int ans = min(total, n - total);
118+
if (n % 2 == 0) {
119+
return ans;
120+
}
121+
for (int i = 0, now = 0; i < n; i++) {
122+
now += s[i] % 2 == i % 2;
123+
int original_front = now;
124+
int original_back = total - original_front;
125+
int changed_front = i + 1 - original_front;
126+
int changed_back = n - i - 1 - original_back;
127+
ans = min(ans, min(original_front + changed_back, changed_front + original_back));
128+
}
129+
return ans;
130+
}
131+
};
132+
133+
#if defined(_WIN32) || defined(__APPLE__)
134+
/*
135+
001000000010
136+
| | | |
137+
101010101010
138+
139+
001000000010
140+
| | |
141+
001010101010
142+
- 右移一位
143+
010101010100
144+
3次不可
145+
146+
4
147+
*/
148+
int main() {
149+
string s;
150+
while (cin >> s) {
151+
Solution sol;
152+
cout << sol.minFlips(s) << endl;
153+
}
154+
return 0;
155+
}
156+
#endif
157+
```
158+
159+
## 解题方法二:滑动窗口
160+
161+
其实也可以把字符串double,然后定长滑动窗口len(s),求每个窗口变成010或101的最小次数。
162+
163+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158813763)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/07/LeetCode%201888.%E4%BD%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AD%97%E7%AC%A6%E4%BA%A4%E6%9B%BF%E7%9A%84%E6%9C%80%E5%B0%91%E5%8F%8D%E8%BD%AC%E6%AC%A1%E6%95%B0/)哦~
164+
>
165+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,10 @@ categories: [自用]
18611861
|||
18621862
|bribery|n. 贿赂,受贿|
18631863
|bamboo shoot|phrase. 竹笋|
1864+
|||
1865+
|mechanics|n. 力学,结构,技巧,机械学|
1866+
|pretentious|adj. 炫耀的,虚夸的,自命不凡的|
1867+
|indignant|adj. 愤慨的,愤怒的|
18641868

18651869
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
18661870
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)