|
| 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> </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> </p> |
| 49 | + |
| 50 | +<p><strong>提示:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>2 <= num.length <= 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) |
0 commit comments