Skip to content

Commit 95a4c48

Browse files
authored
update: 添加问题“1758.生成交替二进制字符串的最少操作数”的代码(并更新其题解) (#1429)
* 1758: WA.cpp (#1427) * 1758: AC.cpp (#1427) - AC,100.00%,32.67% * update: 添加问题“1758.生成交替二进制字符串的最少操作数”的代码(并更新其题解) (#1429) Signed-off-by: LetMeFly666 <Tisfy@qq.com> * fix: header reg only codex found this. #1429 (comment) --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 404f916 commit 95a4c48

9 files changed

Lines changed: 84 additions & 14 deletions

.commitmsg

Lines changed: 0 additions & 1 deletion
This file was deleted.

.vscode/c_cpp_properties.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"includePath": [
2222
"${workspaceFolder}/**"
2323
],
24-
"defines": [],
24+
"defines": [
25+
"_DEBUG"
26+
],
2527
"macFrameworkPath": [
2628
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
2729
],

.vscode/tasks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"label": "C/C++: g++.exe 生成活动文件",
66
"command": "g++.exe",
77
"args": [
8+
"-D_DEBUG",
89
"-g",
910
"${file}",
1011
"-o",
@@ -30,6 +31,7 @@
3031
"label": "C/C++: clang++ build",
3132
"command": "/usr/bin/clang++",
3233
"args": [
34+
"-D_DEBUG",
3335
"-fcolor-diagnostics",
3436
"-fansi-escape-codes",
3537
"-g",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-05 22:13:51
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-05 22:16:57
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int minOperations(string s) {
14+
int c0 = 0, c1 = 0;
15+
for (int i = 0; i < s.size(); i++) {
16+
c0 += s[i] % 2 == i % 2; // 不用减'0'就行
17+
c1 += s[i] % 2 != i % 2;
18+
}
19+
return min(c0, c1);
20+
}
21+
};

Solutions/LeetCode 1582.二进制矩阵中的特殊位置.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: 1582.二进制矩阵中的特殊位置:模拟/记录每行每列1个数
33
date: 2022-09-04 14:59:22
44
tags: [题解, LeetCode, 简单, 数组, 矩阵]
55
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2021/12/23/special1.jpg
67
---
78

89
# 【LetMeFly】1582.二进制矩阵中的特殊位置:模拟/记录每行每列1个数/行有单1再看列
@@ -17,6 +18,8 @@ categories: [题解, LeetCode]
1718

1819
<p><strong>示例 1:</strong></p>
1920

21+
<img src="https://assets.leetcode.com/uploads/2021/12/23/special1.jpg">
22+
2023
<pre><strong>输入:</strong>mat = [[1,0,0],
2124
&nbsp; [0,0,<strong>1</strong>],
2225
&nbsp; [1,0,0]]

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: 1758.生成交替二进制字符串的最少操作数
2+
title: 1758.生成交替二进制字符串的最少操作数:5行代码一次遍历
33
date: 2022-11-29 23:00:13
44
tags: [题解, LeetCode, 简单, 字符串, 模拟, 遍历]
55
categories: [题解, LeetCode]
66
---
77

8-
# 【LetMeFly】1758.生成交替二进制字符串的最少操作数
8+
# 【LetMeFly】1758.生成交替二进制字符串的最少操作数:5行代码一次遍历
99

1010
力扣题目链接:[https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/](https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string/)
1111

@@ -69,6 +69,9 @@ categories: [题解, LeetCode]
6969
#### C++
7070

7171
```cpp
72+
/*
73+
* @LastEditTime: 2022-11-29 22:56:38
74+
*/
7275
class Solution {
7376
public:
7477
int minOperations(string& s) {
@@ -82,5 +85,35 @@ public:
8285
};
8386
```
8487
88+
## 方法二:模拟
89+
90+
要么`奇数`位置全`奇数` `偶数`位置全`偶数`,要么`偶数`位置全`奇数` `奇数`位置全`偶数`。
91+
92+
两种情况两个变量一次遍历算出来取最小的那个就好了。
93+
94+
+ 时间复杂度$O(n)$
95+
+ 空间复杂度$O(1)$
96+
97+
### AC代码
98+
99+
#### C++
100+
101+
```cpp
102+
/*
103+
* @LastEditTime: 2026-03-05 22:16:57
104+
*/
105+
class Solution {
106+
public:
107+
int minOperations(string s) {
108+
int c0 = 0, c1 = 0;
109+
for (int i = 0; i < s.size(); i++) {
110+
c0 += s[i] % 2 == i % 2; // 不用减'0'就行
111+
c1 += s[i] % 2 != i % 2;
112+
}
113+
return min(c0, c1);
114+
}
115+
};
116+
```
117+
85118
> 同步发文于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/)哦~
86119
> Tisfy:[https://letmefly.blog.csdn.net/article/details/128107132](https://letmefly.blog.csdn.net/article/details/128107132)

newSolution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2026-03-01 20:15:38
5+
LastEditTime: 2026-03-05 22:38:46
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -93,7 +93,7 @@ def visit_Attribute(self, node):
9393
with open(toName, 'r+', encoding='utf-8') as f:
9494
content = f.read()
9595
f.seek(0)
96-
header = '#if defined(_WIN32) || defined(__APPLE__)\n' +\
96+
header = '#ifdef _DEBUG\n' +\
9797
'#include "_[1,2]toVector.h"\n' +\
9898
'#endif\n\n'
9999
f.write(header + content)
@@ -357,7 +357,7 @@ def removePrefix(data: str, fileType: str) -> str:# (#1399)
357357
new_comment = '\n'.join(kept)
358358
# 2. 删除紧跟的单行 #if defined(...) #include "_[1,2]toVector.h" #endif
359359
rest = re.sub(
360-
r'^\s*#if\s+defined\([^\)]*\)(?:\s*\|\|\s*defined\([^\)]*\))*\s*\n\s*#include\s+"_\[1,2\]toVector\.h"\s*\n\s*#endif\s*\n?',
360+
r'^\s*#ifdef\s+\w+\s*\n\s*#include\s+"_\[1,2\]toVector\.h"\s*\n\s*#endif\s*\n?',
361361
'',
362362
rest,
363363
count=1

test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-05 09:35:39
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-05 09:36:03
6+
*/
7+
#ifdef _DEBUG
8+
#include <stdio.h>
9+
int main() {
10+
printf("1\n");
11+
return 0;
12+
}
13+
#else
14+
int main() {
15+
16+
}
17+
#endif

toSay.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)