Skip to content

Commit 96483f5

Browse files
committed
add 25 solutions
1 parent a737791 commit 96483f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4120
-172
lines changed

assets/output/0997.md

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ one of these people is secretly the town judge.
2323

2424
If the town judge exists, then:
2525

26-
1. The town judge trusts nobody.
27-
2. Everybody (except for the town judge) trusts the town judge.
28-
3. There is exactly one person that satisfies properties **1** and **2**.
26+
1. The town judge trusts nobody.
27+
2. Everybody (except for the town judge) trusts the town judge.
28+
3. There is exactly one person that satisfies properties **1** and **2**.
2929

3030
You are given an array `trust` where `trust[i] = [ai, bi]` representing that
3131
the person labeled `ai` trusts the person labeled `bi`. If a trust
@@ -35,119 +35,141 @@ does not exist.
3535
Return _the label of the town judge if the town judge exists and can be
3636
identified, or return_`-1` _otherwise_.
3737

38-
39-
4038
**Example 1:**
4139

4240
> Input: n = 2, trust = [[1,2]]
43-
>
41+
>
4442
> Output: 2
4543
4644
**Example 2:**
4745

4846
> Input: n = 3, trust = [[1,3],[2,3]]
49-
>
47+
>
5048
> Output: 3
5149
5250
**Example 3:**
5351

5452
> Input: n = 3, trust = [[1,3],[2,3],[3,1]]
55-
>
53+
>
5654
> Output: -1
5755
5856
**Constraints:**
5957

60-
* `1 <= n <= 1000`
61-
* `0 <= trust.length <= 10^4`
62-
* `trust[i].length == 2`
63-
* All the pairs of `trust` are **unique**.
64-
* `ai != bi`
65-
* `1 <= ai, bi <= n`
66-
58+
- `1 <= n <= 1000`
59+
- `0 <= trust.length <= 10^4`
60+
- `trust[i].length == 2`
61+
- All the pairs of `trust` are **unique**.
62+
- `ai != bi`
63+
- `1 <= ai, bi <= n`
6764

6865
## 题目大意
6966

7067
小镇里有 `n` 个人,按从 `1``n` 的顺序编号。传言称,这些人中有一个暗地里是小镇法官。
7168

7269
如果小镇法官真的存在,那么:
7370

74-
1. 小镇法官不会信任任何人。
75-
2. 每个人(除了小镇法官)都信任这位小镇法官。
76-
3. 只有一个人同时满足属性 **1** 和属性 **2**
71+
1. 小镇法官不会信任任何人。
72+
2. 每个人(除了小镇法官)都信任这位小镇法官。
73+
3. 只有一个人同时满足属性 **1** 和属性 **2**
7774

7875
给你一个数组 `trust` ,其中 `trust[i] = [ai, bi]` 表示编号为 `ai` 的人信任编号为 `bi` 的人。
7976

8077
如果小镇法官存在并且可以确定他的身份,请返回该法官的编号;否则,返回 `-1`
8178

82-
83-
8479
**示例 1:**
8580

86-
>
87-
>
88-
>
89-
>
90-
>
9181
> **输入:** n = 2, trust = [[1,2]]
92-
>
82+
>
9383
> **输出:** 2
94-
>
95-
>
9684
9785
**示例 2:**
9886

99-
>
100-
>
101-
>
102-
>
103-
>
10487
> **输入:** n = 3, trust = [[1,3],[2,3]]
105-
>
88+
>
10689
> **输出:** 3
107-
>
108-
>
10990
11091
**示例 3:**
11192

112-
>
113-
>
114-
>
115-
>
116-
>
11793
> **输入:** n = 3, trust = [[1,3],[2,3],[3,1]]
118-
>
94+
>
11995
> **输出:** -1
120-
>
121-
>
12296
97+
**提示:**
12398

99+
- `1 <= n <= 1000`
100+
- `0 <= trust.length <= 10^4`
101+
- `trust[i].length == 2`
102+
- `trust` 中的所有`trust[i] = [ai, bi]` **互不相同**
103+
- `ai != bi`
104+
- `1 <= ai, bi <= n`
124105

125-
**提示:**
106+
## 解题思路
126107

127-
* `1 <= n <= 1000`
128-
* `0 <= trust.length <= 10^4`
129-
* `trust[i].length == 2`
130-
* `trust` 中的所有`trust[i] = [ai, bi]` **互不相同**
131-
* `ai != bi`
132-
* `1 <= ai, bi <= n`
108+
我们需要判断是否存在一个满足以下条件的法官:
133109

110+
1. 法官**不信任任何人**
111+
2. **所有人(除了法官)都信任法官**
134112

135-
## 解题思路
113+
具体来说,如果某人是法官,他的**出度(信任别人)为 0**,同时他的**入度(被别人信任)为 `n - 1`**
114+
115+
1. 对于每个人,记录他信任的次数(出度)和被别人信任的次数(入度),初始化为 0。
116+
117+
2. **遍历数组 `trust` 更新入度和出度**
118+
119+
- 如果 `trust[i] = [a, b]`,表示 `a` 信任 `b`
120+
- `a` 的出度加 1。
121+
- `b` 的入度加 1。
122+
123+
3. **判断是否存在法官**
124+
125+
- 遍历所有人,找出同时满足以下条件的编号 `i`
126+
- 入度为 `n - 1`
127+
- 出度为 0。
128+
129+
4. 如果找到了这样的编号,则返回它;否则返回 `-1`
136130

137131
#### 复杂度分析
138132

139-
- **时间复杂度**`O()`
140-
- **空间复杂度**`O()`
133+
- **时间复杂度**`O(n + m)`
134+
135+
- 遍历 `trust` 数组更新入度和出度需要 `O(m)`,其中 `m``trust` 的长度。
136+
- 遍历所有节点判断法官需要 `O(n)`,其中 `n` 是小镇人数。
137+
- 总时间复杂度为 `O(n + m)`
138+
139+
- **空间复杂度**`O(n)`,需要存储入度和出度数组
141140

142141
## 代码
143142

144143
```javascript
145-
144+
/**
145+
* @param {number} n
146+
* @param {number[][]} trust
147+
* @return {number}
148+
*/
149+
var findJudge = function (n, trust) {
150+
if (n === 1 && trust.length === 0) return 1; // 特殊情况:只有一个人且没有信任关系
151+
152+
let inDegree = new Array(n + 1).fill(0); // 入度
153+
let outDegree = new Array(n + 1).fill(0); // 出度
154+
155+
for (let [a, b] of trust) {
156+
outDegree[a]++;
157+
inDegree[b]++;
158+
}
159+
160+
for (let i = 1; i <= n; i++) {
161+
if (inDegree[i] === n - 1 && outDegree[i] === 0) {
162+
return i;
163+
}
164+
}
165+
166+
return -1;
167+
};
146168
```
147169

148170
## 相关题目
149171

150172
<!-- prettier-ignore -->
151173
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
152174
| :------: | :------ | :------: | :------ | :------: | :------: |
153-
| 277 | 搜寻名人 🔒 | | [``](/tag/graph.md) [`双指针`](/tag/two-pointers.md) [`交互`](/tag/interactive.md) | 🟠 | [🀄️](https://leetcode.cn/problems/find-the-celebrity) [🔗](https://leetcode.com/problems/find-the-celebrity) |
175+
| 277 | 搜寻名人 🔒 | | [``](/tag/graph.md) [`双指针`](/tag/two-pointers.md) [`交互`](/tag/interactive.md) | 🟠 | [🀄️](https://leetcode.cn/problems/find-the-celebrity) [🔗](https://leetcode.com/problems/find-the-celebrity) |

src/.vuepress/sidebar.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,30 +503,53 @@ export default sidebar({
503503
"children": [
504504
"0901",
505505
"0905",
506+
"0908",
506507
"0909",
508+
"0914",
509+
"0917",
507510
"0918",
508511
"0921",
512+
"0922",
513+
"0925",
514+
"0929",
509515
"0931",
510516
"0933",
517+
"0938",
518+
"0941",
519+
"0942",
520+
"0944",
511521
"0945",
512522
"0946",
513523
"0951",
524+
"0953",
514525
"0954",
515526
"0958",
527+
"0961",
516528
"0962",
529+
"0965",
517530
"0973",
531+
"0976",
532+
"0977",
518533
"0981",
519534
"0986",
520-
"0994"
535+
"0989",
536+
"0993",
537+
"0994",
538+
"0997",
539+
"0999"
521540
]
522541
},
523542
{
524543
"text": "1000-1099",
525544
"collapsible": true,
526545
"children": [
546+
"1002",
527547
"1004",
548+
"1005",
528549
"1008",
529550
"1009",
551+
"1013",
552+
"1018",
530553
"1021",
531554
"1047",
532555
"1049",
@@ -707,6 +730,7 @@ export default sidebar({
707730
"2405",
708731
"2406",
709732
"2410",
733+
"2415",
710734
"2416",
711735
"2458",
712736
"2461",

src/book/two_pointer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
| 1099 | 小于 K 的两数之和 🔒 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`二分查找`](/tag/binary-search.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/two-sum-less-than-k) [🔗](https://leetcode.com/problems/two-sum-less-than-k) |
4545
| 75 | 颜色分类 | [[]](/problem/0075.md) | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟠 | [🀄️](https://leetcode.cn/problems/sort-colors) [🔗](https://leetcode.com/problems/sort-colors) |
4646
| 360 | 有序转化数组 🔒 | | [`数组`](/tag/array.md) [`数学`](/tag/math.md) [`双指针`](/tag/two-pointers.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/sort-transformed-array) [🔗](https://leetcode.com/problems/sort-transformed-array) |
47-
| 977 | 有序数组的平方 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/squares-of-a-sorted-array) [🔗](https://leetcode.com/problems/squares-of-a-sorted-array) |
47+
| 977 | 有序数组的平方 | [[]](/problem/0977.md) | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/squares-of-a-sorted-array) [🔗](https://leetcode.com/problems/squares-of-a-sorted-array) |
4848
| 881 | 救生艇 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/boats-to-save-people) [🔗](https://leetcode.com/problems/boats-to-save-people) |
4949
| 42 | 接雨水 | [[]](/problem/0042.md) | [``](/tag/stack.md) [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) `2+` | 🔴 | [🀄️](https://leetcode.cn/problems/trapping-rain-water) [🔗](https://leetcode.com/problems/trapping-rain-water) |
5050
| 443 | 压缩字符串 | [[]](/problem/0443.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/string-compression) [🔗](https://leetcode.com/problems/string-compression) |
@@ -71,7 +71,7 @@
7171
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
7272
| :------: | :------ | :------: | :------ | :------: | :------: |
7373
| 350 | 两个数组的交集 II | [[]](/problem/0350.md) | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`双指针`](/tag/two-pointers.md) `2+` | 🟢 | [🀄️](https://leetcode.cn/problems/intersection-of-two-arrays-ii) [🔗](https://leetcode.com/problems/intersection-of-two-arrays-ii) |
74-
| 925 | 长按键入 | | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/long-pressed-name) [🔗](https://leetcode.com/problems/long-pressed-name) |
74+
| 925 | 长按键入 | [[]](/problem/0925.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/long-pressed-name) [🔗](https://leetcode.com/problems/long-pressed-name) |
7575
| 844 | 比较含退格的字符串 | [[]](/problem/0844.md) | [``](/tag/stack.md) [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/backspace-string-compare) [🔗](https://leetcode.com/problems/backspace-string-compare) |
7676
| 1229 | 安排会议日程 🔒 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`排序`](/tag/sorting.md) | 🟠 | [🀄️](https://leetcode.cn/problems/meeting-scheduler) [🔗](https://leetcode.com/problems/meeting-scheduler) |
7777
| 415 | 字符串相加 | [[]](/problem/0415.md) | [`数学`](/tag/math.md) [`字符串`](/tag/string.md) [`模拟`](/tag/simulation.md) | 🟢 | [🀄️](https://leetcode.cn/problems/add-strings) [🔗](https://leetcode.com/problems/add-strings) |

src/plan/company_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ headerDepth: 0
6464
| 138 | 随机链表的复制 | [[]](/problem/0138.md) | [`哈希表`](/tag/hash-table.md) [`链表`](/tag/linked-list.md) | 🟠 | [🀄️](https://leetcode.cn/problems/copy-list-with-random-pointer) [🔗](https://leetcode.com/problems/copy-list-with-random-pointer) | 19 |
6565
| 124 | 二叉树中的最大路径和 | [[]](/problem/0124.md) | [``](/tag/tree.md) [`深度优先搜索`](/tag/depth-first-search.md) [`动态规划`](/tag/dynamic-programming.md) `1+` | 🔴 | [🀄️](https://leetcode.cn/problems/binary-tree-maximum-path-sum) [🔗](https://leetcode.com/problems/binary-tree-maximum-path-sum) | 19 |
6666
| 1268 | 搜索推荐系统 | [[]](/problem/1268.md) | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
67-
| 953 | 验证外星语词典 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
67+
| 953 | 验证外星语词典 | [[]](/problem/0953.md) | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
6868
| 973 | 最接近原点的 K 个点 | [[]](/problem/0973.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) `4+` | 🟠 | [🀄️](https://leetcode.cn/problems/k-closest-points-to-origin) [🔗](https://leetcode.com/problems/k-closest-points-to-origin) | 18 |
6969
| 29 | 两数相除 | [[]](/problem/0029.md) | [`位运算`](/tag/bit-manipulation.md) [`数学`](/tag/math.md) | 🟠 | [🀄️](https://leetcode.cn/problems/divide-two-integers) [🔗](https://leetcode.com/problems/divide-two-integers) | 18 |
7070
| 636 | 函数的独占时间 | | [``](/tag/stack.md) [`数组`](/tag/array.md) | 🟠 | [🀄️](https://leetcode.cn/problems/exclusive-time-of-functions) [🔗](https://leetcode.com/problems/exclusive-time-of-functions) | 18 |

0 commit comments

Comments
 (0)