Skip to content

Commit a79e876

Browse files
committed
update: 添加问题“3340.检查平衡字符串”的代码和题解(#809)
feat: #809 暂未验证,因为当前脚本使用的是 92df623 版 Signed-off-by: LetMeFly666 <[email protected]>
1 parent 92df623 commit a79e876

10 files changed

+264
-1
lines changed

.commitmsg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
feat: #809
3+
暂未验证,因为当前脚本使用的是 92df62367f78ad67cb781d7b4df8d7b27a272cbe 版

.nodeploy

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-14 09:30:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-14 09:32:51
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
bool isBalanced(string num) {
14+
int cnt = 0;
15+
for (int i = 0; i < num.size(); i++) {
16+
cnt += i % 2 ? (num[i] - '0') : -(num[i] - '0');
17+
}
18+
return cnt == 0;
19+
}
20+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-14 09:36:55
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-14 09:38:01
6+
*/
7+
package main
8+
9+
func isBalanced(num string) bool {
10+
cnt := 0
11+
for i, c := range num {
12+
if i % 2 == 0 {
13+
cnt += int(c) - 48
14+
} else {
15+
cnt -= int(c) - 48
16+
}
17+
}
18+
return cnt == 0
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-14 09:35:26
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-14 09:35:26
6+
*/
7+
class Solution {
8+
public boolean isBalanced(String num) {
9+
int cnt = 0;
10+
for (int i = 0; i < num.length(); i++) {
11+
if (i % 2 == 0) {
12+
cnt += num.charAt(i) - 48;
13+
} else {
14+
cnt -= num.charAt(i) - 48;
15+
}
16+
}
17+
return cnt == 0;
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-14 09:34:04
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-14 09:34:04
6+
'''
7+
class Solution:
8+
def isBalanced(self, num: str) -> bool:
9+
cnt = 0
10+
for i, c in enumerate(num):
11+
cnt += ord(c) - 48 if i % 2 else 48 - ord(c)
12+
return cnt == 0

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@
921921
|3297.统计重新排列后包含另一个字符串的子字符串数目I|中等|<a href="https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/09/LeetCode%203297.%E7%BB%9F%E8%AE%A1%E9%87%8D%E6%96%B0%E6%8E%92%E5%88%97%E5%90%8E%E5%8C%85%E5%90%AB%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145031494" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/solutions/3042787/letmefly-3297tong-ji-zhong-xin-pai-lie-h-8ele/" target="_blank">LeetCode题解</a>|
922922
|3305.元音辅音字符串计数I|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/12/LeetCode%203305.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146197747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/solutions/3607571/letmefly-3305yuan-yin-fu-yin-zi-fu-chuan-ptkg/" target="_blank">LeetCode题解</a>|
923923
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
924+
|3340.检查平衡字符串|简单|<a href="https://leetcode.cn/problems/check-balanced-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146249653" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-balanced-string/solutions/3610899/letmefly-3340jian-cha-ping-heng-zi-fu-ch-p8eo/" target="_blank">LeetCode题解</a>|
924925
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
925926
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
926927
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: 3340.检查平衡字符串:模拟
3+
date: 2025-03-14 09:39:25
4+
tags: [题解, LeetCode, 简单, 字符串, 模拟]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3340.检查平衡字符串:模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/check-balanced-string/](https://leetcode.cn/problems/check-balanced-string/)
11+
12+
<p>给你一个仅由数字 0 - 9 组成的字符串 <code>num</code>。如果偶数下标处的数字之和等于奇数下标处的数字之和,则认为该数字字符串是一个 <b>平衡字符串</b>。</p>
13+
14+
<p>如果 <code>num</code> 是一个 <strong>平衡字符串</strong>,则返回 <code>true</code>;否则,返回 <code>false</code>。</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong class="example">示例 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>输入:</strong>num<span class="example-io"> = "1234"</span></p>
22+
23+
<p><strong>输出:</strong><span class="example-io">false</span></p>
24+
25+
<p><strong>解释:</strong></p>
26+
27+
<ul>
28+
<li>偶数下标处的数字之和为 <code>1 + 3 = 4</code>,奇数下标处的数字之和为 <code>2 + 4 = 6</code>。</li>
29+
<li>由于 4 不等于 6,<code>num</code> 不是平衡字符串。</li>
30+
</ul>
31+
</div>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>输入:</strong>num<span class="example-io"> = "24123"</span></p>
37+
38+
<p><strong>输出:</strong>true</p>
39+
40+
<p><strong>解释:</strong></p>
41+
42+
<ul>
43+
<li>偶数下标处的数字之和为 <code>2 + 1 + 3 = 6</code>,奇数下标处的数字之和为 <code>4 + 2 = 6</code>。</li>
44+
<li>由于两者相等,<code>num</code> 是平衡字符串。</li>
45+
</ul>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong>提示:</strong></p>
51+
52+
<ul>
53+
<li><code>2 &lt;= num.length &lt;= 100</code></li>
54+
<li><code>num</code> 仅由数字 0 - 9 组成。</li>
55+
</ul>
56+
57+
58+
59+
## 解题方法:遍历求和
60+
61+
使用一个整型变量$cnt$来统计结果即可。遍历字符串,遇到奇数下标则加上当前字符对应的数字,否则减去之。最终判断$cnt$是否为$0$。
62+
63+
+ 时间复杂度$O(len(num))$
64+
+ 空间复杂度$O(1)$
65+
66+
### AC代码
67+
68+
#### C++
69+
70+
```cpp
71+
/*
72+
* @Author: LetMeFly
73+
* @Date: 2025-03-14 09:30:43
74+
* @LastEditors: LetMeFly.xyz
75+
* @LastEditTime: 2025-03-14 09:32:51
76+
*/
77+
class Solution {
78+
public:
79+
bool isBalanced(string num) {
80+
int cnt = 0;
81+
for (int i = 0; i < num.size(); i++) {
82+
cnt += i % 2 ? (num[i] - '0') : -(num[i] - '0');
83+
}
84+
return cnt == 0;
85+
}
86+
};
87+
```
88+
89+
#### Python
90+
91+
```python
92+
'''
93+
Author: LetMeFly
94+
Date: 2025-03-14 09:34:04
95+
LastEditors: LetMeFly.xyz
96+
LastEditTime: 2025-03-14 09:34:04
97+
'''
98+
class Solution:
99+
def isBalanced(self, num: str) -> bool:
100+
cnt = 0
101+
for i, c in enumerate(num):
102+
cnt += ord(c) - 48 if i % 2 else 48 - ord(c)
103+
return cnt == 0
104+
```
105+
106+
#### Java
107+
108+
```java
109+
/*
110+
* @Author: LetMeFly
111+
* @Date: 2025-03-14 09:35:26
112+
* @LastEditors: LetMeFly.xyz
113+
* @LastEditTime: 2025-03-14 09:35:26
114+
*/
115+
class Solution {
116+
public boolean isBalanced(String num) {
117+
int cnt = 0;
118+
for (int i = 0; i < num.length(); i++) {
119+
if (i % 2 == 0) {
120+
cnt += num.charAt(i) - 48;
121+
} else {
122+
cnt -= num.charAt(i) - 48;
123+
}
124+
}
125+
return cnt == 0;
126+
}
127+
}
128+
```
129+
130+
#### Go
131+
132+
```go
133+
/*
134+
* @Author: LetMeFly
135+
* @Date: 2025-03-14 09:36:55
136+
* @LastEditors: LetMeFly.xyz
137+
* @LastEditTime: 2025-03-14 09:38:01
138+
*/
139+
package main
140+
141+
func isBalanced(num string) bool {
142+
cnt := 0
143+
for i, c := range num {
144+
if i % 2 == 0 {
145+
cnt += int(c) - 48
146+
} else {
147+
cnt -= int(c) - 48
148+
}
149+
}
150+
return cnt == 0
151+
}
152+
```
153+
154+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/146249653)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/)哦~
155+
>
156+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

categoryWhenNewSolution.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-14 09:49:08
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-14 09:51:50
6+
Command: python categoryWhenNewSolution.py 3340. 检查平衡字符串
7+
'''
8+
import sys
9+
import time
10+
11+
12+
argv = sys.argv
13+
num = int(argv[1][:-1])
14+
title = ""
15+
for i in range(2, len(argv)):
16+
if i != 2:
17+
title += " "
18+
title += argv[i]
19+
nameProblem = "AllProblems/{0}.{1}.md".format(num, title)
20+
21+
with open(nameProblem, "r", encoding="utf-8") as f:
22+
problem = f.read()
23+
24+
solution = problem
25+
def refreshPublistTime(solution: str) -> str:
26+
splited = solution.split("\n")
27+
splited[2] = "date: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
28+
splited.insert(4, 'categories: [题解, LeetCode]')
29+
return "\n".join(splited)
30+
31+
solution = refreshPublistTime(solution)
32+
print(solution)

newSolution.py

Lines changed: 2 additions & 1 deletion
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: 2025-03-12 10:40:29
5+
LastEditTime: 2025-03-14 09:49:25
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -126,6 +126,7 @@ def genSolutionPart(num):
126126
def refreshPublistTime(solution: str) -> str:
127127
splited = solution.split("\n")
128128
splited[2] = "date: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
129+
splited.insert(4, 'categories: [题解, LeetCode]')
129130
return "\n".join(splited)
130131

131132
solution = refreshPublistTime(solution)

0 commit comments

Comments
 (0)