Skip to content

Commit b9e6cea

Browse files
committed
add 761
1 parent 2afb2ec commit b9e6cea

File tree

7 files changed

+176
-29
lines changed

7 files changed

+176
-29
lines changed

‎.pre-commit-config.yaml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.3.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
- id: check-json
12+
- repo: https://github.com/pre-commit/mirrors-prettier
13+
rev: ""
14+
hooks:
15+
- id: prettier

‎README.md‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# 📓 LeetCode笔记
1+
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
2+
3+
# 📓 LeetCode 笔记
24

35
本笔记适用于目标在于掌握**基础算法**的工程师
46

@@ -31,11 +33,11 @@ collect some good articles about data structure and algorithm.
3133

3234
## 🙉 说明
3335

34-
* 本项目题目来源:[力扣(LeetCode)](https://leetcode.cn)
35-
* 题目著作权归[力扣(LeetCode)](https://leetcode.cn) 所有。商业转载请联系官方授权,非商业转载请注明出处。
36-
* 题解语言不限,专注于算法实现
36+
- 本项目题目来源:[力扣(LeetCode)](https://leetcode.cn)
37+
- 题目著作权归[力扣(LeetCode)](https://leetcode.cn) 所有。商业转载请联系官方授权,非商业转载请注明出处。
38+
- 题解语言不限,专注于算法实现
3739

38-
## 🌲目录
40+
## 🌲 目录
3941

4042
```tree
4143
.
@@ -60,9 +62,8 @@ node script deploy.sh
6062

6163
## 🔐 Problems & Solutions
6264

63-
完成进度([1075](./TOC-By-ID.md)🔑/ [2736](https://leetcode.cn/problemset/all/)🔒)
65+
完成进度([1075](./TOC-By-ID.md)🔑/ [2736](https://leetcode.cn/problemset/all/)🔒)
6466

6567
- 🔗 [标签查找](./TOC-By-Tag.md)
6668

6769
- 🔗 [题号查找](./TOC-By-ID.md)
68-

‎algorithms/101-200/187.重复的DNA序列.md‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [187.重复的DNA序列](https://leetcode.cn/problems/repeated-dna-sequences/)
1+
# [187.重复的 DNA 序列](https://leetcode.cn/problems/repeated-dna-sequences/)
22

33
<p><strong>DNA序列</strong>&nbsp;由一系列核苷酸组成,缩写为<meta charset="UTF-8" />&nbsp;<code>'A'</code>,&nbsp;<code>'C'</code>,&nbsp;<code>'G'</code>&nbsp;和<meta charset="UTF-8" />&nbsp;<code>'T'</code>.。</p>
44

@@ -35,7 +35,6 @@
3535
<li><code>s[i]</code><code>==</code><code>'A'</code>、<code>'C'</code>、<code>'G'</code>&nbsp;or&nbsp;<code>'T'</code></li>
3636
</ul>
3737

38-
3938
<details>
4039
<summary>标签:</summary>
4140
['位运算', '哈希表', '字符串', '滑动窗口', '哈希函数', '滚动哈希']
@@ -46,12 +45,11 @@
4645
喜欢:392
4746
</details>
4847

48+
---
4949

50-
----------
51-
52-
# 算法1
50+
# 算法 1
5351

54-
## (暴力枚举) $O(n^2)$
52+
## (暴力枚举) $O(n^2)$
5553

5654
blablabla
5755

@@ -68,5 +66,3 @@ blablabla
6866
```
6967

7068
## 参考文献
71-
72-

‎algorithms/201-300/201.数字范围按位与.md‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
3434
</ul>
3535

36-
3736
<details>
3837
<summary>标签:</summary>
3938
['位运算']
@@ -44,12 +43,11 @@
4443
喜欢:393
4544
</details>
4645

46+
---
4747

48-
----------
49-
50-
# 算法1
48+
# 算法 1
5149

52-
## (暴力枚举) $O(n^2)$
50+
## (暴力枚举) $O(n^2)$
5351

5452
blablabla
5553

@@ -66,5 +64,3 @@ blablabla
6664
```
6765

6866
## 参考文献
69-
70-

‎algorithms/201-300/202.快乐数.md‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
4242
</ul>
4343

44-
4544
<details>
4645
<summary>标签:</summary>
4746
['哈希表', '数学', '双指针']
@@ -52,12 +51,11 @@
5251
喜欢:1011
5352
</details>
5453

54+
---
5555

56-
----------
57-
58-
# 算法1
56+
# 算法 1
5957

60-
## (暴力枚举) $O(n^2)$
58+
## (暴力枚举) $O(n^2)$
6159

6260
blablabla
6361

@@ -74,5 +72,3 @@ blablabla
7472
```
7573

7674
## 参考文献
77-
78-
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# [761.特殊的二进制序列](https://leetcode.cn/problems/special-binary-string/)
2+
3+
<p>特殊的二进制序列是具有以下两个性质的二进制序列:</p>
4+
5+
<ul>
6+
<li>0 的数量与 1 的数量相等。</li>
7+
<li>二进制序列的每一个前缀码中 1 的数量要大于等于 0 的数量。</li>
8+
</ul>
9+
10+
<p>给定一个特殊的二进制序列&nbsp;<code>S</code>,以字符串形式表示。定义一个<em>操作 </em>为首先选择&nbsp;<code>S</code>&nbsp;的两个连续且非空的特殊的子串,然后将它们交换。(两个子串为连续的当且仅当��一个子串的最后一个字符恰好为第二个子串的第一个字符的前一个字符。)</p>
11+
12+
<p>在任意次数的操作之后,交换后的字符串按照字典序排列的最大的结果是什么?</p>
13+
14+
<p><strong>示例 1:</strong></p>
15+
16+
<pre>
17+
<strong>输入:</strong> S = &quot;11011000&quot;
18+
<strong>输出:</strong> &quot;11100100&quot;
19+
<strong>解释:</strong>
20+
将子串 &quot;10&quot; (在S[1]出现) 和 &quot;1100&quot; (在S[3]出现)进行交换。
21+
这是在进行若干次操作后按字典序排列最大的结果。
22+
</pre>
23+
24+
<p><strong>说明:</strong></p>
25+
26+
<ol>
27+
<li><code>S</code>&nbsp;的长度不超过&nbsp;<code>50</code>。</li>
28+
<li><code>S</code>&nbsp;保证为一个满足上述定义的<em>特殊 </em>的二进制序列。</li>
29+
</ol>
30+
31+
<details>
32+
<summary>标签:</summary>
33+
['递归', '字符串']
34+
</details>
35+
36+
<details>
37+
<summary>难度:Hard</summary>
38+
喜欢:176
39+
</details>
40+
41+
---
42+
43+
# 算法 1
44+
45+
## 括号序列 + 递归 $O(n^2logn)$
46+
47+
### 算法思路
48+
49+
1. 先将字符串划分成同级括号序列
50+
2. 将同级括号序列进行排序,得到字典序最小的序列
51+
3. 递归上述过程
52+
53+
### 前置知识
54+
55+
括号序列:[剑指 Offer II 085. 生成匹配的括号](https://leetcode.cn/problems/IDBivT/)
56+
57+
括号序列排序: [剑指 Offer 45. 把数组排成最小的数](https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/)
58+
59+
## 时间复杂度
60+
61+
递归分治分成 $n$ 层
62+
63+
每层排序 $nlogn$
64+
65+
总时间复杂度:$O(n^2logn)$
66+
67+
## 代码实现
68+
69+
```java []
70+
71+
```
72+
73+
```cpp []
74+
class Solution {
75+
public:
76+
string makeLargestSpecial(string S) {
77+
if (S.size() <=2) return S;
78+
vector<string> q; // 记录 同级括号,用来排序
79+
string s;
80+
int cnt = 0 ; // 判断 同级括号
81+
for (char c: S) {
82+
s+=c;
83+
if (c=='1') cnt++;
84+
else {
85+
cnt--;
86+
if (cnt == 0) {
87+
q.push_back('1' + makeLargestSpecial(s.substr(1, s.size()-2)) + '0');
88+
s.clear();
89+
}
90+
}
91+
}
92+
sort(q.begin(), q.end(), [](string&a, string&b){ // 同级括号 字典序最小
93+
return a + b > b+a;
94+
});
95+
string res ;
96+
for (auto x: q) res += x;
97+
return res;
98+
}
99+
};
100+
```
101+
102+
## 参考文献
103+
104+
# 相关题目
105+
106+
## [AcWing 889. 满足条件的 01 序列](https://www.acwing.com/problem/content/891/)
107+
108+
## [剑指 Offer II 085. 生成匹配的括号](https://leetcode.cn/problems/IDBivT/)
109+
110+
## [剑指 Offer 45. 把数组排成最小的数](https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
重新定义 sort 运算
2+
3+
```java
4+
class Solution {
5+
public String minNumber(int[] nums) {
6+
List<Integer> w = Arrays.stream(nums).boxed().sorted(new Comparator<Integer>() {
7+
@Override
8+
public int compare(Integer o1, Integer o2) {
9+
return (String.valueOf(o1) + o2).compareTo(String.valueOf(o2) + o1);
10+
}
11+
}).collect(Collectors.toList());
12+
13+
final List<String> res = w.stream().map(String::valueOf).collect(Collectors.toList());
14+
return String.join("", res);
15+
}
16+
}
17+
```
18+
19+
```cpp
20+
class Solution {
21+
public:
22+
static bool cmp(int a, int b){
23+
string as = to_string(a), bs = to_string(b);
24+
return as + bs < bs + as;
25+
}
26+
string minNumber(vector<int>& nums) {
27+
sort(nums.begin(), nums.end(), cmp);
28+
string x;
29+
for(auto s: nums) x+= to_string(s);
30+
return x;
31+
}
32+
};
33+
```

0 commit comments

Comments
 (0)