Skip to content

Commit c170fe0

Browse files
Merge pull request #18 from SharingSource/ac_oier
✨feat: Add 401、1600、剑指 Offer 38
2 parents 407fa69 + ef173b3 commit c170fe0

File tree

9 files changed

+390
-1
lines changed

9 files changed

+390
-1
lines changed

Index/双指针.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | ---------- |
33
| [3. 无重复字符的最长子串 ](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/shua-chuan-lc-shuang-zhi-zhen-ha-xi-biao-q08m/) | 中等 | 🤩🤩🤩🤩🤩 |
4-
| [11. 盛最多水的容器 ](https://leetcode-cn.com/problems/container-with-most-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) | 中等 | 🤩🤩🤩🤩🤩 |
4+
| [11. 盛最多水的容器 ](https://leetcode-cn.com/problems/container-with-most-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/container-with-most-water/solution/shua-chuan-lc-shuang-zhi-zhen-tan-xin-ji-52gf/) | 中等 | 🤩🤩🤩🤩🤩 |
55
| [15. 三数之和](https://leetcode-cn.com/problems/3sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/3sum/solution/shua-chuan-lc-pai-xu-shuang-zhi-zhen-jie-cd8r/) | 中等 | 🤩🤩🤩🤩🤩 |
66
| [16. 最接近的三数之和](https://leetcode-cn.com/problems/3sum-closest/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/3sum-closest/solution/shua-chuan-lc-pai-xu-shuang-zhi-zhen-jie-p2ou/) | 中等 | 🤩🤩🤩🤩 |
77
| [18. 四数之和](https://leetcode-cn.com/problems/4sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/4sum/solution/shua-chuan-lc-pai-xu-shuang-zhi-zhen-jie-dqx7/) | 中等 | 🤩🤩🤩🤩 |

Index/哈希表.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [1074. 元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/) | 困难 | 🤩🤩🤩 |
1919
| [1178. 猜字谜](https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/solution/xiang-jin-zhu-shi-xiang-jie-po-su-wei-yu-3cr2/) | 困难 | 🤩🤩🤩🤩 |
2020
| [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩 |
21+
| [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/) | 中等 | 🤩🤩🤩 |
2122
| [1603. 设计停车系统](https://leetcode-cn.com/problems/design-parking-system/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-parking-system/solution/yi-ti-san-jie-jian-dan-bian-liang-ha-xi-0gs72/) | 简单 | 🤩🤩 |
2223
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/mo-ni-ti-po-su-jie-fa-by-ac_oier-ff69/) | 简单 | 🤩🤩 |
2324

Index/回溯算法.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
| [90. 子集 II](https://leetcode-cn.com/problems/subsets-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subsets-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-g77q/) | 中等 | 🤩🤩🤩🤩 |
88
| [131. 分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/palindrome-partitioning/solution/wei-sha-yao-zhe-yang-bao-sou-ya-shi-ru-h-41gf/) | 中等 | 🤩🤩🤩🤩 |
99
| [301. 删除无效的括号](https://leetcode-cn.com/problems/remove-invalid-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-invalid-parentheses/solution/yi-fen-zhong-nei-kan-dong-jiang-gua-hao-aya6k/) | 困难 | 🤩🤩🤩🤩 |
10+
| [剑指 Offer 38. 字符串的排列](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/solution/gong-shui-san-xie-tong-yong-shi-xian-qu-4jbkj/) | 中等 | 🤩🤩🤩🤩 |
1011

Index/打表.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| 题目 | 题解 | 难度 | 推荐指数 |
2+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [401. 二进制手表](https://leetcode-cn.com/problems/binary-watch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-watch/solution/gong-shui-san-xie-jian-dan-ti-xue-da-bia-gwn2/) | 简单 | 🤩🤩🤩🤩🤩 |
4+

Index/链表.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
| [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lru-cache/solution/gong-shui-san-xie-she-ji-shu-ju-jie-gou-68hv2/) | 中等 | 🤩🤩🤩🤩🤩 |
1515
| [203. 移除链表元素](https://leetcode-cn.com/problems/remove-linked-list-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-linked-list-elements/solution/gong-shui-san-xie-yi-chu-lian-biao-yuan-ca6fu/) | 简单 | 🤩🤩🤩 |
1616
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |
17+
| [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/) | 中等 | 🤩🤩🤩 |
1718

LeetCode/1231-1240/1239. 串联字符串的最大长度(中等).md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class Solution {
236236

237237
不难发现,答案与最优前缀序列是一对多关系,这指导我们可以将「参数」调得宽松一些。
238238

239+
**具有「一对多」关系的问题十分适合使用「模拟退火」,使用「模拟退火」可以轻松将本题 `arr.length` 数据范围上升到 $60$ 甚至以上。**
240+
239241
调整成比较宽松的参数可以跑赢「二进制枚举」,但为了以后增加数据不容易被 hack,还是使用 `N=400` & `fa=0.90` 的搭配。
240242

241243
「模拟退火」的几个参数的作用在 [这里](https://leetcode-cn.com/problems/find-minimum-time-to-finish-all-jobs/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-4epdd/) 说过了,不再赘述。
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/)** ,难度为 **中等**
4+
5+
Tag : 「单链表」、「哈希表」
6+
7+
8+
9+
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。
10+
11+
这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。
12+
```
13+
Successor(x, curOrder):
14+
如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
15+
如果 x 是国王,那么返回 null
16+
否则,返回 Successor(x 的父亲, curOrder)
17+
否则,返回 x 不在 curOrder 中最年长的孩子
18+
```
19+
比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。
20+
21+
1. 一开始, curOrder 为 ["king"].
22+
2. 调用 Successor(king, curOrder) ,返回 Alice ,所以我们将 Alice 放入 curOrder 中,得到 ["king", "Alice"] 。
23+
3. 调用 Successor(Alice, curOrder) ,返回 Jack ,所以我们将 Jack 放入 curOrder 中,得到 ["king", "Alice", "Jack"] 。
24+
4. 调用 Successor(Jack, curOrder) ,返回 Bob ,所以我们将 Bob 放入 curOrder 中,得到 ["king", "Alice", "Jack", "Bob"] 。
25+
5. 调用 Successor(Bob, curOrder) ,返回 null 。最终得到继承顺序为 ["king", "Alice", "Jack", "Bob"] 。
26+
通过以上的函数,我们总是能得到一个唯一的继承顺序。
27+
28+
请你实现 ThroneInheritance 类:
29+
30+
* ThroneInheritance(string kingName) 初始化一个 ThroneInheritance 类的对象。国王的名字作为构造函数的参数传入。
31+
* void birth(string parentName, string childName) 表示 parentName 新拥有了一个名为 childName 的孩子。
32+
* void death(string name) 表示名为 name 的人死亡。一个人的死亡不会影响 Successor 函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。
33+
* string[] getInheritanceOrder() 返回 除去 死亡人员的当前继承顺序列表。
34+
35+
示例:
36+
```
37+
输入:
38+
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
39+
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
40+
41+
输出:
42+
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
43+
44+
解释:
45+
ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:king
46+
t.birth("king", "andy"); // 继承顺序:king > andy
47+
t.birth("king", "bob"); // 继承顺序:king > andy > bob
48+
t.birth("king", "catherine"); // 继承顺序:king > andy > bob > catherine
49+
t.birth("andy", "matthew"); // 继承顺序:king > andy > matthew > bob > catherine
50+
t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > alex > catherine
51+
t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > asha > catherine
52+
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
53+
t.death("bob"); // 继承顺序:king > andy > matthew > bob(已经去世)> alex > asha > catherine
54+
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]
55+
```
56+
57+
提示:
58+
* 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
59+
* kingName,parentName, childName 和 name 仅包含小写英文字母。
60+
* 所有的参数 childName 和 kingName 互不相同。
61+
* 所有 death 函数中的死亡名字 name 要么是国王,要么是已经出生了的人员名字。
62+
* 每次调用 birth(parentName, childName) 时,测试用例都保证 parentName 对应的人员是活着的。
63+
* 最多调用 $10^5$ 次birth 和 death 。
64+
* 最多调用 10 次 getInheritanceOrder 。
65+
66+
---
67+
68+
### 单向链表 & 标记删除
69+
70+
根据题意,我们需要将「新儿子」插入到「父亲」的「最后一个儿子」的「儿子们」的后面(**注意这是个递归过程**);如果该「父亲」还没有任何儿子,则直接插到「父亲」后面。
71+
72+
因此,我们需要在节点 `Node` 中使用一个 `last` 记录该节点的「最后一个儿子」,同时因为删除的时候,我们无法在 $O(1)$ 的复杂度内更新 `last` 信息,所以只能使用「标记删除」的方式。
73+
74+
![image.png](https://pic.leetcode-cn.com/1624150709-JoDfDY-image.png)
75+
76+
代码:
77+
```Java []
78+
class ThroneInheritance {
79+
class Node {
80+
String name;
81+
Node next;
82+
Node last; // 记录最后一个儿子
83+
boolean isDeleted = false;
84+
Node (String _name) {
85+
name = _name;
86+
}
87+
}
88+
Map<String, Node> map = new HashMap<>();
89+
Node head = new Node(""), tail = new Node("");
90+
public ThroneInheritance(String name) {
91+
Node root = new Node(name);
92+
root.next = tail;
93+
head.next = root;
94+
map.put(name, root);
95+
}
96+
97+
public void birth(String pname, String cname) {
98+
Node node = new Node(cname);
99+
map.put(cname, node);
100+
Node p = map.get(pname);
101+
Node tmp = p;
102+
while (tmp.last != null) tmp = tmp.last;
103+
node.next = tmp.next;
104+
tmp.next = node;
105+
p.last = node;
106+
}
107+
108+
public void death(String name) {
109+
Node node = map.get(name);
110+
node.isDeleted = true;
111+
}
112+
113+
public List<String> getInheritanceOrder() {
114+
List<String> ans = new ArrayList<>();
115+
Node tmp = head.next;
116+
while (tmp.next != null) {
117+
if (!tmp.isDeleted) ans.add(tmp.name);
118+
tmp = tmp.next;
119+
}
120+
return ans;
121+
}
122+
}
123+
```
124+
* 时间复杂度:`birth``getInheritanceOrder` 操作为 $O(n)$;其余操作为 $O(1)$
125+
* 时间复杂度:$O(n)$
126+
127+
---
128+
129+
### 最后
130+
131+
这是我们「刷穿 LeetCode」系列文章的第 `No.1600` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
132+
133+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
134+
135+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
136+
137+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
138+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[401. 二进制手表](https://leetcode-cn.com/problems/binary-watch/solution/gong-shui-san-xie-jian-dan-ti-xue-da-bia-gwn2/)** ,难度为 **简单**
4+
5+
Tag : 「打表」、「二进制」
6+
7+
8+
9+
二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。
10+
11+
例如,下面的二进制手表读取 "3:25" 。
12+
13+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/03/29/binary_clock_samui_moon.jpg)
14+
(图源:WikiMedia - Binary clock samui moon.jpg ,许可协议:Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) )
15+
16+
给你一个整数 turnedOn ,表示当前亮着的 LED 的数量,返回二进制手表可以表示的所有可能时间。你可以 按任意顺序 返回答案。
17+
18+
小时不会以零开头:
19+
20+
例如,"01:00" 是无效的时间,正确的写法应该是 "1:00" 。
21+
分钟必须由两位数组成,可能会以零开头:
22+
23+
例如,"10:2" 是无效的时间,正确的写法应该是 "10:02" 。
24+
25+
26+
示例 1:
27+
28+
输入:turnedOn = 1
29+
输出:["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
30+
示例 2:
31+
32+
输入:turnedOn = 9
33+
输出:[]
34+
35+
36+
提示:
37+
* 0 <= turnedOn <= 10
38+
39+
---
40+
41+
### 打表
42+
43+
具体的,我们可以创建一个 `静态数据结构` 来存储打表信息(需确保全局唯一,即使存在多组测试数据只生成一次打表数据)。
44+
45+
然后在返回数据的时候直接 $O(1)$ 查表返回。
46+
47+
PS. 如果打表逻辑计算量接近 $10^7$ 上限,可以考虑放到本地去做,这里数据量较少,直接放到 `static` 代码块去做即可。
48+
49+
代码:
50+
```Java []
51+
class Solution {
52+
// 打表逻辑,也可以放到本地做
53+
// 注意使用 static 修饰,确保打表数据只会被生成一次
54+
static Map<Integer, List<String>> map = new HashMap<>();
55+
static {
56+
for (int h = 0; h <= 11; h++) {
57+
for (int m = 0; m <= 59; m++) {
58+
int tot = getCnt(h) + getCnt(m);
59+
List<String> list = map.getOrDefault(tot, new ArrayList<String>());
60+
list.add(h + ":" + (m <= 9 ? "0" + m : m));
61+
map.put(tot, list);
62+
}
63+
}
64+
}
65+
static int getCnt(int x) {
66+
int ans = 0;
67+
for (int i = x; i > 0; i -= lowbit(i)) ans++;
68+
return ans;
69+
}
70+
static int lowbit(int x) {
71+
return x & -x;
72+
}
73+
public List<String> readBinaryWatch(int t) {
74+
return map.getOrDefault(t, new ArrayList<>());
75+
}
76+
}
77+
```
78+
* 时间复杂度:$O(1)$
79+
* 空间复杂度:$O(n)$
80+
81+
82+
---
83+
84+
### 最后
85+
86+
这是我们「刷穿 LeetCode」系列文章的第 `No.401` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
87+
88+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
89+
90+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
91+
92+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
93+

0 commit comments

Comments
 (0)