Skip to content

Commit 7adf0fc

Browse files
authored
update: 添加问题“761.特殊的二进制字符串”的代码和题解 (#1401)
* 761: AC.cpp (#1400) + en cpp - AC,100.00%,6.45% * update: 添加问题“761.特殊的二进制字符串”的代码和题解 (#1401) Signed-off-by: Tisfy <Tisfy@foxmail.com> * fix: Windows cmd cd F:xx needs F: first --------- Signed-off-by: Tisfy <Tisfy@foxmail.com>
1 parent 50aa399 commit 7adf0fc

9 files changed

Lines changed: 283 additions & 190 deletions

.commitTitleExtra

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

.commitmsg

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-20 11:00:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-20 11:12:06
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
11011000
13+
(()(()))
14+
( () (()) )
15+
*/
16+
class Solution {
17+
private:
18+
vector<pair<int, int>> split(string& s) {
19+
vector<pair<int, int>> ans;
20+
int diff = 0, from = 0;
21+
for (int i = 0; i < s.size(); i++) {
22+
if (s[i] == '1') {
23+
diff++;
24+
} else {
25+
diff--;
26+
}
27+
if (!diff) {
28+
ans.push_back({from, i});
29+
from = i + 1;
30+
}
31+
}
32+
return ans;
33+
}
34+
public:
35+
string makeLargestSpecial(string s) {
36+
if (s.empty()) {
37+
return s;
38+
}
39+
vector<pair<int, int>> pairs = split(s);
40+
if (pairs.size() == 1) {
41+
return '1' + makeLargestSpecial(s.substr(1, s.size() - 2)) + '0';
42+
}
43+
vector<string> parts;
44+
parts.reserve(pairs.size());
45+
for (auto [l, r] : pairs) {
46+
parts.push_back(makeLargestSpecial(s.substr(l, r - l + 1)));
47+
}
48+
sort(parts.begin(), parts.end(), greater<>());
49+
string ans;
50+
for (string& part : parts) {
51+
ans += part;
52+
}
53+
return ans;
54+
}
55+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-02-20 11:19:26
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-02-20 11:21:04
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
11011000
13+
(()(()))
14+
( () (()) )
15+
*/
16+
class Solution {
17+
private:
18+
vector<pair<int, int>> split(string& s) {
19+
vector<pair<int, int>> ans;
20+
int diff = 0, from = 0;
21+
for (int i = 0; i < s.size(); i++) {
22+
if (s[i] == '1') {
23+
diff++;
24+
} else {
25+
diff--;
26+
}
27+
if (!diff) {
28+
ans.push_back({from, i});
29+
from = i + 1;
30+
}
31+
}
32+
return ans;
33+
}
34+
public:
35+
string makeLargestSpecial(string s) {
36+
if (s.empty()) {
37+
return s;
38+
}
39+
vector<pair<int, int>> pairs = split(s);
40+
vector<string> parts;
41+
parts.reserve(pairs.size());
42+
for (auto [l, r] : pairs) {
43+
parts.push_back('1' + makeLargestSpecial(s.substr(l + 1, r - l - 1)) + '0');
44+
}
45+
sort(parts.begin(), parts.end(), greater<>());
46+
string ans;
47+
for (string& part : parts) {
48+
ans += part;
49+
}
50+
return ans;
51+
}
52+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@
409409
|0749.隔离病毒|困难|<a href="https://leetcode.cn/problems/contain-virus/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/18/LeetCode%200749.%E9%9A%94%E7%A6%BB%E7%97%85%E6%AF%92/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125846470" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/contain-virus/solution/letmefly-749ge-chi-bing-du-by-tisfy-746u/" target="_blank">LeetCode题解</a>|
410410
|0754.到达终点数字|中等|<a href="https://leetcode.cn/problems/reach-a-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/04/LeetCode%200754.%E5%88%B0%E8%BE%BE%E7%BB%88%E7%82%B9%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127684453" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reach-a-number/solutions/1947583/letmefly-754dao-da-zhong-dian-shu-zi-by-z6q4r/" target="_blank">LeetCode题解</a>|
411411
|0756.金字塔转换矩阵|中等|<a href="https://leetcode.cn/problems/pyramid-transition-matrix/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/29/LeetCode%200756.%E9%87%91%E5%AD%97%E5%A1%94%E8%BD%AC%E6%8D%A2%E7%9F%A9%E9%98%B5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156400643" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pyramid-transition-matrix/solutions/3868875/letmefly-756jin-zi-ta-zhuan-huan-ju-zhen-ldxx/" target="_blank">LeetCode题解</a>|
412+
|0761.特殊的二进制字符串|困难|<a href="https://leetcode.cn/problems/special-binary-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/02/20/LeetCode%200761.%E7%89%B9%E6%AE%8A%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/158235045" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/special-binary-string/solutions/3905163/letmefly-761te-shu-de-er-jin-zhi-zi-fu-c-q26c/" target="_blank">LeetCode题解</a>|
412413
|0765.情侣牵手|困难|<a href="https://leetcode.cn/problems/couples-holding-hands/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/11/LeetCode%200765.%E6%83%85%E4%BE%A3%E7%89%B5%E6%89%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134355602" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/couples-holding-hands/solutions/2522691/letmefly-765qing-lu-qian-shou-yan-du-you-nt6z/" target="_blank">LeetCode题解</a>|
413414
|0769.最多能完成排序的块|中等|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/13/LeetCode%200769.%E6%9C%80%E5%A4%9A%E8%83%BD%E5%AE%8C%E6%88%90%E6%8E%92%E5%BA%8F%E7%9A%84%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127295302" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/solution/letmefly-769zui-duo-neng-wan-cheng-pai-x-pzsz/" target="_blank">LeetCode题解</a>|
414415
|0771.宝石与石头|简单|<a href="https://leetcode.cn/problems/jewels-and-stones/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/24/LeetCode%200771.%E5%AE%9D%E7%9F%B3%E4%B8%8E%E7%9F%B3%E5%A4%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131888350" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/jewels-and-stones/solutions/2356279/letmefly-771bao-shi-yu-shi-tou-by-tisfy-fqmc/" target="_blank">LeetCode题解</a>|
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: 761.特殊的二进制字符串:分治(左右括号对移动)
3+
date: 2026-02-20 11:23:43
4+
tags: [题解, LeetCode, 困难, 字符串, 分治, 排序, 递归, DFS]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】761.特殊的二进制字符串:分治(左右括号对移动)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/special-binary-string/](https://leetcode.cn/problems/special-binary-string/)
11+
12+
<p><strong>特殊的二进制字符串</strong> 是具有以下两个性质的二进制序列:</p>
13+
14+
<ul>
15+
<li><code>0</code> 的数量与 <code>1</code> 的数量相等。</li>
16+
<li>二进制序列的每一个前缀码中 <code>1</code> 的数量要大于等于 <code>0</code> 的数量。</li>
17+
</ul>
18+
19+
<p>给定一个特殊的二进制字符串&nbsp;<code>s</code>。</p>
20+
21+
<p>一次移动操作包括选择字符串 <code>s</code> 中的两个连续的、非空的、特殊子串,并交换它们。两个字符串是连续的,如果第一个字符串的最后一个字符与第二个字符串的第一个字符的索引相差正好为 1。</p>
22+
23+
<p>返回在字符串上应用任意次操作后可能得到的字典序最大的字符串。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例 1:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong> S = "11011000"
31+
<strong>输出:</strong> "11100100"
32+
<strong>解释:</strong>
33+
将子串 "10" (在 s[1] 出现) 和 "1100" (在 s[3] 出现)进行交换。
34+
这是在进行若干次操作后按字典序排列最大的结果。
35+
</pre>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<pre>
40+
<b>输入:</b>s = "10"
41+
<b>输出:</b>"10"
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
50+
<li><code>s[i]</code>&nbsp;为&nbsp;<code>'0'</code> 或&nbsp;<code>'1'</code>。</li>
51+
<li><code>s</code>&nbsp;是一个特殊的二进制字符串。</li>
52+
</ul>
53+
54+
55+
56+
## 解题方法:分治
57+
58+
例如示例一:`11011000`,可以看成`(()(()))`
59+
60+
我们要做的就是保持括号处于匹配状态下,让对应的01字符串字典序尽可能大。
61+
62+
匹配字符串有两种组合方式:
63+
64+
1. 拼接,如`() + (()) = ()(())`
65+
2. 嵌套,在`()(())`外面套一个括号变成`(()(()))`
66+
67+
这解法不就来了,我们可以反其道而行之,把``(()(()))`拆成`( () (()) )`:
68+
69+
> 对于最外层括号里的`()``(())`,当然是`(())`放到`()`前面比较好(`1100`放到`10`前面字典序更大)
70+
>
71+
> 这不就是天然的递归么,`(()(()))`只有一个“拼接的括号”,外层总体顺序不变,内层`()(())`递归:
72+
>
73+
> 内层`()(())`有两个“拼接的括号”,两个拼接的括号分别递归,直到递归字符串为空递归停止,并将两个递归完成的“拼接括号”排序重新拼接到一起,变成`(())()`
74+
75+
+ 时间复杂度$O(n^2\log n)$,递归像树一样,每次执行有一个排序和字符串拼接
76+
77+
```
78+
(()(()))
79+
|
80+
()(())
81+
/ \
82+
'' ()
83+
```
84+
85+
+ 空间复杂度$O(n)$
86+
87+
### AC代码
88+
89+
#### C++
90+
91+
```cpp
92+
/*
93+
* @LastEditTime: 2026-02-20 11:21:04
94+
*/
95+
/*
96+
11011000
97+
(()(()))
98+
( () (()) )
99+
*/
100+
class Solution {
101+
private:
102+
vector<pair<int, int>> split(string& s) {
103+
vector<pair<int, int>> ans;
104+
int diff = 0, from = 0;
105+
for (int i = 0; i < s.size(); i++) {
106+
if (s[i] == '1') {
107+
diff++;
108+
} else {
109+
diff--;
110+
}
111+
if (!diff) {
112+
ans.push_back({from, i});
113+
from = i + 1;
114+
}
115+
}
116+
return ans;
117+
}
118+
public:
119+
string makeLargestSpecial(string s) {
120+
if (s.empty()) {
121+
return s;
122+
}
123+
vector<pair<int, int>> pairs = split(s);
124+
vector<string> parts;
125+
parts.reserve(pairs.size());
126+
for (auto [l, r] : pairs) {
127+
parts.push_back('1' + makeLargestSpecial(s.substr(l + 1, r - l - 1)) + '0');
128+
}
129+
sort(parts.begin(), parts.end(), greater<>());
130+
string ans;
131+
for (string& part : parts) {
132+
ans += part;
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/158235045)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/02/20/LeetCode%200761.%E7%89%B9%E6%AE%8A%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/)~
140+
>
141+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,8 @@ categories: [自用]
18231823
|||
18241824
|Brazilian|adj. 巴西的<br/>n. 巴西人|
18251825
|infringe|v. 侵犯,违背|
1826+
|||
1827+
|mumps|n. 腮腺炎|
18261828

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

testDaka.bash.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: 2026-02-19 18:57:58
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-02-20 12:04:31
6+
'''
7+
import os
8+
9+
problem_name = 't'
10+
problem_url = '1'
11+
daka_url = '2'
12+
13+
# os.system(f'cd tryGoPy && bash -c pwd')
14+
# exit()
15+
16+
# os.system(' bash -c "cmd.exe /c \\\"echo 1\\\"" ')
17+
# exit()
18+
19+
os.system(f'cd F:/OtherApps/Program/Git/Store/Store2_Web_Various/various && bash -c "source /f/FromNextcloud/sync/TFpath/let.sh && lets git tisfy" && pushhere hi 1 && bash -c "source /f/FromNextcloud/sync/TFpath/let.sh && lets git hub"')
20+
21+
"""
22+
Windows上bash如何执行一行命令
23+
24+
25+
os.system(f"bash -c \"source /f/FromNextcloud/sync/TFpath/let.sh && cd /f/OtherApps/Program/Git/Store/Store2_Web_Various/various && lets git tisfy && lets git hub\"")
26+
现在我需要在lets git tisfy && lets git hub中间加上个环境变量中的pushhere.bat的调用 pushhere hi ni
27+
28+
29+
30+
31+
f'bash -c "source /f/FromNextcloud/sync/TFpath/let.sh && cd /f/OtherApps/Program/Git/Store/Store2_Web_Various/various && lets git tisfy && cmd.exe /c \\"%pushhere% \'{problem_name}[{problem_url}] 打卡链接:{daka_url}\'\\" && lets git hub"'
32+
"""

0 commit comments

Comments
 (0)