Skip to content

Commit b00d942

Browse files
committed
update: 添加问题“1963.使字符串平衡的最小交换次数”的代码和题解(#820)
docs: 柠檬微趣题解 - 小改 word: en words Signed-off-by: LetMeFly666 <[email protected]>
1 parent d1857df commit b00d942

9 files changed

+304
-1
lines changed

.commitmsg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
docs: 柠檬微趣题解 - 小改
2+
3+
word: en words
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-17 12:16:25
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-17 12:17:39
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int minSwaps(string s) {
14+
int zuoQianYou = 0, zuo = 0;
15+
for (char c : s) {
16+
if (c == '[') {
17+
zuo++;
18+
} else {
19+
if (zuo) {
20+
zuo--;
21+
} else {
22+
zuoQianYou++;
23+
}
24+
}
25+
}
26+
return (zuoQianYou + 1) / 2;
27+
}
28+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-17 12:20:26
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-17 12:21:31
6+
*/
7+
package main
8+
9+
func minSwaps(s string) int {
10+
zuoQianYou, zuo := 0, 0
11+
for i := range s {
12+
if s[i] == '[' {
13+
zuo++
14+
} else {
15+
if zuo > 0 {
16+
zuo--
17+
} else {
18+
zuoQianYou++
19+
}
20+
}
21+
}
22+
return (zuoQianYou + 1) / 2
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-17 12:21:49
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-17 12:21:49
6+
*/
7+
class Solution {
8+
public int minSwaps(String s) {
9+
int zuoQianYou = 0, zuo = 0;
10+
for (char c : s.toCharArray()) {
11+
if (c == '[') {
12+
zuo++;
13+
} else {
14+
if (zuo > 0) {
15+
zuo--;
16+
} else {
17+
zuoQianYou++;
18+
}
19+
}
20+
}
21+
return (zuoQianYou + 1) / 2;
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-17 12:18:42
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-17 12:18:42
6+
'''
7+
class Solution:
8+
def minSwaps(self, s: str) -> int:
9+
zuoQianYou = zuo = 0
10+
for c in s:
11+
if c == '[':
12+
zuo += 1
13+
else:
14+
if zuo:
15+
zuo -= 1
16+
else:
17+
zuoQianYou += 1
18+
return (zuoQianYou + 1) // 2

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@
630630
|1954.收集足够苹果的最小花园周长|中等|<a href="https://leetcode.cn/problems/minimum-garden-perimeter-to-collect-enough-apples/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/24/LeetCode%201954.%E6%94%B6%E9%9B%86%E8%B6%B3%E5%A4%9F%E8%8B%B9%E6%9E%9C%E7%9A%84%E6%9C%80%E5%B0%8F%E8%8A%B1%E5%9B%AD%E5%91%A8%E9%95%BF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135183907" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-garden-perimeter-to-collect-enough-apples/solutions/2578282/letmefly-1954shou-ji-zu-gou-ping-guo-de-l8j92/" target="_blank">LeetCode题解</a>|
631631
|1958.检查操作是否合法|中等|<a href="https://leetcode.cn/problems/check-if-move-is-legal/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/07/LeetCode%201958.%E6%A3%80%E6%9F%A5%E6%93%8D%E4%BD%9C%E6%98%AF%E5%90%A6%E5%90%88%E6%B3%95/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140249215" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-move-is-legal/solutions/2834234/letmefly-1958jian-cha-cao-zuo-shi-fou-he-xic6/" target="_blank">LeetCode题解</a>|
632632
|1962.移除石子使总数最小|中等|<a href="https://leetcode.cn/problems/remove-stones-to-minimize-the-total/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/23/LeetCode%201962.%E7%A7%BB%E9%99%A4%E7%9F%B3%E5%AD%90%E4%BD%BF%E6%80%BB%E6%95%B0%E6%9C%80%E5%B0%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135165032" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-stones-to-minimize-the-total/solutions/2576721/letmefly-1962yi-chu-shi-zi-shi-zong-shu-o7lmr/" target="_blank">LeetCode题解</a>|
633+
|1963.使字符串平衡的最小交换次数|中等|<a href="https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/17/LeetCode%201963.%E4%BD%BF%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B9%B3%E8%A1%A1%E7%9A%84%E6%9C%80%E5%B0%8F%E4%BA%A4%E6%8D%A2%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146312262" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/solutions/3615488/letmefly-1963shi-zi-fu-chuan-ping-heng-d-lsqv/" target="_blank">LeetCode题解</a>|
633634
|1969.数组元素的最小非零乘积|中等|<a href="https://leetcode.cn/problems/minimum-non-zero-product-of-the-array-elements/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/20/LeetCode%201969.%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%B0%8F%E9%9D%9E%E9%9B%B6%E4%B9%98%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136872322" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-non-zero-product-of-the-array-elements/solutions/2697806/letmefly-1969shu-zu-yuan-su-de-zui-xiao-u7llx/" target="_blank">LeetCode题解</a>|
634635
|1971.寻找图中是否存在路径|简单|<a href="https://leetcode.cn/problems/find-if-path-exists-in-graph/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/19/LeetCode%201971.%E5%AF%BB%E6%89%BE%E5%9B%BE%E4%B8%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128377260" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-if-path-exists-in-graph/solutions/2026262/letmefly-1971xun-zhao-tu-zhong-shi-fou-c-uunl/" target="_blank">LeetCode题解</a>|
635636
|1976.到达目的地的方案数|中等|<a href="https://leetcode.cn/problems/number-of-ways-to-arrive-at-destination/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/05/LeetCode%201976.%E5%88%B0%E8%BE%BE%E7%9B%AE%E7%9A%84%E5%9C%B0%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136481215" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-ways-to-arrive-at-destination/" target="_blank">LeetCode题解</a>|
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
---
2+
title: 1963.使字符串平衡的最小交换次数:计数模拟(不需要麻烦的“三种写法一步步优化”)
3+
date: 2025-03-17 12:24:38
4+
tags: [题解, LeetCode, 中等, 栈, 贪心, 双指针, 字符串, 计数, 模拟, 括号匹配]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1963.使字符串平衡的最小交换次数:计数模拟(不需要麻烦的“三种写法一步步优化”)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/](https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/)
11+
12+
<p>给你一个字符串 <code>s</code> ,<strong>下标从 0 开始</strong> ,且长度为偶数 <code>n</code> 。字符串 <strong>恰好</strong> 由 <code>n / 2</code> 个开括号 <code>'['</code> 和 <code>n / 2</code> 个闭括号 <code>']'</code> 组成。</p>
13+
14+
<p>只有能满足下述所有条件的字符串才能称为 <strong>平衡字符串</strong> :</p>
15+
16+
<ul>
17+
<li>字符串是一个空字符串,或者</li>
18+
<li>字符串可以记作 <code>AB</code> ,其中 <code>A</code> 和 <code>B</code> 都是 <strong>平衡字符串</strong> ,或者</li>
19+
<li>字符串可以写成 <code>[C]</code> ,其中 <code>C</code> 是一个 <strong>平衡字符串</strong> 。</li>
20+
</ul>
21+
22+
<p>你可以交换 <strong>任意</strong> 两个下标所对应的括号 <strong>任意</strong> 次数。</p>
23+
24+
<p>返回使<em> </em><code>s</code> 变成 <strong>平衡字符串</strong> 所需要的 <strong>最小</strong> 交换次数。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong>示例 1:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>s = "][]["
32+
<strong>输出:</strong>1
33+
<strong>解释:</strong>交换下标 0 和下标 3 对应的括号,可以使字符串变成平衡字符串。
34+
最终字符串变成 "[[]]" 。
35+
</pre>
36+
37+
<p><strong>示例 2:</strong></p>
38+
39+
<pre>
40+
<strong>输入:</strong>s = "]]][[["
41+
<strong>输出:</strong>2
42+
<strong>解释:</strong>执行下述操作可以使字符串变成平衡字符串:
43+
- 交换下标 0 和下标 4 对应的括号,s = "[]][][" 。
44+
- 交换下标 1 和下标 5 对应的括号,s = "[[][]]" 。
45+
最终字符串变成 "[[][]]" 。
46+
</pre>
47+
48+
<p><strong>示例 3:</strong></p>
49+
50+
<pre>
51+
<strong>输入:</strong>s = "[]"
52+
<strong>输出:</strong>0
53+
<strong>解释:</strong>这个字符串已经是平衡字符串。
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
58+
<p><strong>提示:</strong></p>
59+
60+
<ul>
61+
<li><code>n == s.length</code></li>
62+
<li><code>2 &lt;= n &lt;= 10<sup>6</sup></code></li>
63+
<li><code>n</code> 为偶数</li>
64+
<li><code>s[i]</code> 为<code>'['</code> 或 <code>']'</code></li>
65+
<li>开括号 <code>'['</code> 的数目为 <code>n / 2</code> ,闭括号 <code>']'</code> 的数目也是 <code>n / 2</code></li>
66+
</ul>
67+
68+
69+
70+
## 解题方法:计数模拟
71+
72+
题目保证`[``]`的数量是相同的,也就是说一定可以通过交换达成配对。
73+
74+
使用两个变量:$zuoQianYou$(左前右)和$zuo$分别记录“`[``]`”的数量和“未被消耗的`[`”的数量。
75+
76+
遍历字符串
77+
78+
+ 如果当前字符是`[`,就说明后续可以有一个`]`来和它配对并将他“消费”,$zuo+=1$
79+
+ 如果当前字符是`]`,就看有无`[`以供配对:
80+
81+
+ 如果$zuo\gt 0$,说明有未配对的`[`,消耗之($zuo-=1$)
82+
+ 否则(没有未配对的`[`),$zuoQianYou+=1$
83+
84+
存在“无法配对的`[``]`”就说明要发生交换。但是注意$zuoQianYou$个“`[``]`”只需要交换$\lceil\frac{zuoQianYou}{2}\rceil$次,因为`]`和后面某个`[`交换后,实际上`[`的数量也加一了,顺便就还能与一个`]`配对。
85+
86+
+ 时间复杂度$O(len(s))$
87+
+ 空间复杂度$O(1)$
88+
89+
对于<span title="https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/solutions/922728/go-tan-xin-by-endlesscheng-7h9n/">某“三种写法,一步步优化”</span>,看似看完恍然大悟,实则完全没必要那么麻烦。因为这“一步步优化”实则大概是其作者的思考过程罢了。若能直接想到简单方法,就直接想吧!尝试几次就能发现之前想法可能存在的漏洞了。但不可否认这位作者绝大多数文章还是十分优质的。
90+
91+
### AC代码
92+
93+
#### C++
94+
95+
```cpp
96+
/*
97+
* @Author: LetMeFly
98+
* @Date: 2025-03-17 12:16:25
99+
* @LastEditors: LetMeFly.xyz
100+
* @LastEditTime: 2025-03-17 12:17:39
101+
*/
102+
class Solution {
103+
public:
104+
int minSwaps(string s) {
105+
int zuoQianYou = 0, zuo = 0;
106+
for (char c : s) {
107+
if (c == '[') {
108+
zuo++;
109+
} else {
110+
if (zuo) {
111+
zuo--;
112+
} else {
113+
zuoQianYou++;
114+
}
115+
}
116+
}
117+
return (zuoQianYou + 1) / 2;
118+
}
119+
};
120+
```
121+
122+
#### Python
123+
124+
```python
125+
'''
126+
Author: LetMeFly
127+
Date: 2025-03-17 12:18:42
128+
LastEditors: LetMeFly.xyz
129+
LastEditTime: 2025-03-17 12:18:42
130+
'''
131+
class Solution:
132+
def minSwaps(self, s: str) -> int:
133+
zuoQianYou = zuo = 0
134+
for c in s:
135+
if c == '[':
136+
zuo += 1
137+
else:
138+
if zuo:
139+
zuo -= 1
140+
else:
141+
zuoQianYou += 1
142+
return (zuoQianYou + 1) // 2
143+
```
144+
145+
#### Java
146+
147+
```java
148+
/*
149+
* @Author: LetMeFly
150+
* @Date: 2025-03-17 12:21:49
151+
* @LastEditors: LetMeFly.xyz
152+
* @LastEditTime: 2025-03-17 12:21:49
153+
*/
154+
class Solution {
155+
public int minSwaps(String s) {
156+
int zuoQianYou = 0, zuo = 0;
157+
for (char c : s.toCharArray()) {
158+
if (c == '[') {
159+
zuo++;
160+
} else {
161+
if (zuo > 0) {
162+
zuo--;
163+
} else {
164+
zuoQianYou++;
165+
}
166+
}
167+
}
168+
return (zuoQianYou + 1) / 2;
169+
}
170+
}
171+
```
172+
173+
#### Go
174+
175+
```go
176+
/*
177+
* @Author: LetMeFly
178+
* @Date: 2025-03-17 12:20:26
179+
* @LastEditors: LetMeFly.xyz
180+
* @LastEditTime: 2025-03-17 12:21:31
181+
*/
182+
package main
183+
184+
func minSwaps(s string) int {
185+
zuoQianYou, zuo := 0, 0
186+
for i := range s {
187+
if s[i] == '[' {
188+
zuo++
189+
} else {
190+
if zuo > 0 {
191+
zuo--
192+
} else {
193+
zuoQianYou++
194+
}
195+
}
196+
}
197+
return (zuoQianYou + 1) / 2
198+
}
199+
```
200+
201+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/146312262)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/03/17/LeetCode%201963.%E4%BD%BF%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B9%B3%E8%A1%A1%E7%9A%84%E6%9C%80%E5%B0%8F%E4%BA%A4%E6%8D%A2%E6%AC%A1%E6%95%B0/)哦~
202+
>
203+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Nowcoder-NingMengWeiQu-20250316-YiMian.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public:
166166
>
167167
> ![0031力扣官方题解动图](https://assets.leetcode.cn/solution-static/31/31.gif)
168168
>
169+
> <!-- https://i-blog.csdnimg.cn/img_convert/cfe8dab382d906ee772abc57b9ab951e.gif -->
170+
>
169171
> 首先找到最后一个“后面有比他大的数”的数(假设下标为$i$),接着找$i$后面最后一个大于它的数(假设下标为$j$),交换元素$i$、$j$
170172
>
171173
> 这样,对于交换的两个数,就满足了“最小数尽可能靠右”、“最大数尽可能小”。
@@ -563,7 +565,7 @@ int main() {
563565
564566
## End
565567
566-
**考完之后**搜考题的时候搜到了一个(帖子)[https://www.nowcoder.com/discuss/497532413803773952],竟然是原题!
568+
**考完之后**搜考题的时候搜到了一个[帖子](https://www.nowcoder.com/discuss/497532413803773952),竟然是原题!
567569
568570
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/146304076)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/03/16/Nowcoder-NingMengWeiQu-20250316-YiMian/)哦~
569571
>

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,8 @@ categories: [自用]
10471047
|bourgeois|adj. 中产阶级的,追求物质享受的,世俗的,资产阶级的|
10481048
|||
10491049
|magistrate|n. 地方执法官|
1050+
|||
1051+
|fertilizer|n. 肥料,促进发展者|
10501052

10511053
<p class="wordCounts">单词收录总数</p>
10521054

0 commit comments

Comments
 (0)