Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17283d0

Browse files
committed
Deploying to main from @ 2xiao/leetcode-js@9df5a8f 🚀
1 parent febdb25 commit 17283d0

Some content is hidden

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

58 files changed

+4437
-312
lines changed

‎plan/contest_list.md

Lines changed: 25 additions & 25 deletions
Large diffs are not rendered by default.

‎plan/top_300_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ headerDepth: 0
575575
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
576576
| :------: | :------ | :------: | :------ | :------: | :------: |
577577
| 1232 | 缀点成线 | [[]](/problem/1232.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) | 🟢 | [🀄️](https://leetcode.cn/problems/check-if-it-is-a-straight-line) [🔗](https://leetcode.com/problems/check-if-it-is-a-straight-line) |
578-
| 1266 | 访问所有点的最小时间 | | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) | 🟢 | [🀄️](https://leetcode.cn/problems/minimum-time-visiting-all-points) [🔗](https://leetcode.com/problems/minimum-time-visiting-all-points) |
578+
| 1266 | 访问所有点的最小时间 | [[]](/problem/1266.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) | 🟢 | [🀄️](https://leetcode.cn/problems/minimum-time-visiting-all-points) [🔗](https://leetcode.com/problems/minimum-time-visiting-all-points) |
579579
| 892 | 三维形体的表面积 | [[]](/problem/0892.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/surface-area-of-3d-shapes) [🔗](https://leetcode.com/problems/surface-area-of-3d-shapes) |
580580
| 1401 | 圆和矩形是否有重叠 | | [`几何`](/tag/geometry.md) [`数学`](/tag/math.md) | 🟠 | [🀄️](https://leetcode.cn/problems/circle-and-rectangle-overlapping) [🔗](https://leetcode.com/problems/circle-and-rectangle-overlapping) |
581581
| 963 | 最小面积矩形 II | | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) | 🟠 | [🀄️](https://leetcode.cn/problems/minimum-area-rectangle-ii) [🔗](https://leetcode.com/problems/minimum-area-rectangle-ii) |

‎problem/0443.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,6 @@ var compress = function (chars) {
184184
| 38 | 外观数列 | [[]](/problem/0038.md) | [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/count-and-say) [🔗](https://leetcode.com/problems/count-and-say) |
185185
| 271 | 字符串的编码与解码 🔒 | | [`设计`](/tag/design.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/encode-and-decode-strings) [🔗](https://leetcode.com/problems/encode-and-decode-strings) |
186186
| 604 | 迭代压缩字符串 🔒 | | [`设计`](/tag/design.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/design-compressed-string-iterator) [🔗](https://leetcode.com/problems/design-compressed-string-iterator) |
187-
| 1313 | 解压缩编码列表 | | [`数组`](/tag/array.md) | 🟢 | [🀄️](https://leetcode.cn/problems/decompress-run-length-encoded-list) [🔗](https://leetcode.com/problems/decompress-run-length-encoded-list) |
187+
| 1313 | 解压缩编码列表 | [[]](/problem/1313.md) | [`数组`](/tag/array.md) | 🟢 | [🀄️](https://leetcode.cn/problems/decompress-run-length-encoded-list) [🔗](https://leetcode.com/problems/decompress-run-length-encoded-list) |
188188
| 3163 | 压缩字符串 III | [[]](/problem/3163.md) | [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/string-compression-iii) [🔗](https://leetcode.com/problems/string-compression-iii) |
189189
| 3167 | 字符串的更好压缩 🔒 | | [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) [`计数`](/tag/counting.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/better-compression-of-string) [🔗](https://leetcode.com/problems/better-compression-of-string) |

‎problem/0830.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,63 @@ Return _the intervals of every **large** group sorted in **increasing order by s
110110

111111
## 解题思路
112112

113+
可以通过一次遍历字符串,记录连续字符的起点和终点,当字符发生变化时判断这段连续字符的长度是否大于等于 3,从而找到大组。
114+
115+
1. **初始化变量**:
116+
117+
- `start` 表示当前连续字符的起始位置。
118+
- 遍历字符串时,用 `end` 表示当前字符的位置。
119+
- 使用结果数组 `res` 存储所有大组的起始和结束位置。
120+
121+
2. **遍历字符串**:
122+
123+
- 如果当前字符 `s[end]` 与前一个字符 `s[start]` 不同:
124+
- 检查从 `start``end - 1` 的字符数量是否大于等于 3:
125+
- 如果是,将 `[start, end - 1]` 添加到结果数组。
126+
-`start` 更新为 `end`,即新一段连续字符的起始位置。
127+
128+
3. **处理最后一组字符**:
129+
130+
- 如果字符串结尾的最后一组字符长度大于等于 3,将其位置添加到结果数组。
131+
132+
4. **返回结果**:
133+
- 返回存储大组位置的数组 `res`
134+
113135
#### 复杂度分析
114136

115-
- **时间复杂度**:`O()`,
116-
- **空间复杂度**:`O()`,
137+
- **时间复杂度**:`O(n)`,其中 `n` 是字符串的长度,遍历字符串一次。
138+
- **空间复杂度**:`O(n)`,结果数组 `res` 存储所有大组的位置,最坏情况下,所有字符形成一个大组。
117139

118140
## 代码
119141

120142
```javascript
121-
143+
/**
144+
* @param {string} s
145+
* @return {number[][]}
146+
*/
147+
var largeGroupPositions = function (s) {
148+
const n = s.length;
149+
let res = [];
150+
let start = 0;
151+
152+
// 遍历字符串
153+
for (let end = 0; end < n; end++) {
154+
// 当字符变化时,处理当前段
155+
if (s[end] !== s[start]) {
156+
if (end - start > 2) {
157+
res.push([start, end - 1]);
158+
}
159+
start = end; // 更新起始位置
160+
}
161+
}
162+
163+
// 处理最后一组字符
164+
if (n - start > 2) {
165+
res.push([start, n - 1]);
166+
}
167+
168+
return res;
169+
};
122170
```
123171

124172
## 相关题目

‎problem/0946.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,83 @@ pop operations on an initially empty stack, or_`false` _otherwise._
5656

5757
## 题目大意
5858

59-
给定 `pushed``popped` 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 `push` 和弹出 `pop` 操作序列的结果时,返回 `true`;否则,返回 `false`
59+
给定 `pushed``popped` 两个序列,每个序列中的 **值都不重复** ,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop
60+
操作序列的结果时,返回 `true`;否则,返回 `false`
61+
62+
**示例 1:**
63+
64+
> **输入:** pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
65+
>
66+
> **输出:** true
67+
>
68+
> **解释:** 我们可以按以下顺序执行:
69+
>
70+
> push(1), push(2), push(3), push(4), pop() -> 4,
71+
>
72+
> push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
73+
74+
**示例 2:**
75+
76+
> **输入:** pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
77+
>
78+
> **输出:** false
79+
>
80+
> **解释:** 1 不能在 2 之前弹出。
81+
82+
**提示:**
83+
84+
- `1 <= pushed.length <= 1000`
85+
- `0 <= pushed[i] <= 1000`
86+
- `pushed` 的所有元素 **互不相同**
87+
- `popped.length == pushed.length`
88+
- `popped``pushed` 的一个排列
6089

6190
## 解题思路
6291

63-
这道题可以使用模拟栈的方法进行判断,使用一个辅助栈 `stack`,模拟 `push``pop` 操作,最后通过判断栈是否为空来得到最终的结果。
92+
这道题可以使用模拟栈的方法进行判断,使用一个辅助栈 `stack`,模拟 入栈(`push`)和出栈(`pop`)操作,最后通过判断栈是否为空来来验证序列的正确性。
93+
94+
1. **初始化:**
6495

65-
具体思路如下:
96+
- 创建一个空栈 `stack` 来模拟栈的操作。
97+
- 设置一个变量 `popIndex` 为 0,表示当前需要匹配的出栈序列中的元素位置。
6698

67-
1. 遍历 `pushed` 数组,模拟入栈操作,并在每次入栈后,判断是否需要执行出栈操作。
68-
2. 如果当前栈顶元素等于 `popped` 数组中下一个要出栈的元素,则执行出栈操作。
69-
3. 遍历结束后,判断栈是否为空。
99+
2. **遍历 `pushed`:**
100+
101+
- 将每个元素 `item` 入栈(`stack.push(item)`)。
102+
- 在每次入栈后,检查栈顶元素是否与 `popped[popIndex]` 相等:
103+
- 如果相等,说明当前出栈序列中的元素可以出栈,我们执行 `stack.pop()` 并将 `popIndex` 加 1。
104+
- 继续重复这个过程,直到栈顶元素与 `popped[popIndex]` 不相等或栈为空。
105+
106+
3. **验证结果:**
107+
- 如果最终栈为空,说明 `popped` 是合法的出栈序列;否则不是。
108+
109+
#### 复杂度分析
110+
111+
- **时间复杂度**:`O(n)`,其中 `n``pushed` 的长度,每个元素入栈一次,最多出栈一次,总操作次数是 `O(n)`,
112+
- **空间复杂度**:`O(n)`,栈最多存储 `pushed` 中的所有元素。
70113

71114
## 代码
72115

73116
```javascript
74-
117+
/**
118+
* @param {number[]} pushed
119+
* @param {number[]} popped
120+
* @return {boolean}
121+
*/
122+
var validateStackSequences = function (pushed, popped) {
123+
let stack = []; // 模拟栈操作
124+
let popIndex = 0; // 指向出栈序列的当前位置
125+
126+
for (let item of pushed) {
127+
stack.push(item); // 入栈操作
128+
// 栈顶元素与当前出栈元素相等时,执行出栈操作
129+
while (stack.length > 0 && stack[stack.length - 1] === popped[popIndex]) {
130+
stack.pop();
131+
popIndex++;
132+
}
133+
}
134+
135+
// 如果栈为空,表示出栈序列是合法的
136+
return stack.length === 0;
137+
};
75138
```

‎problem/0997.md

Lines changed: 68 additions & 55 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,114 +35,127 @@ 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+
**提示:**
98+
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`
123105

106+
## 解题思路
124107

125-
**提示:**
108+
1.**初始化数据结构:**
126109

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`
110+
- 创建一个 `judge` 数组,长度为 `n + 1`,用来记录每个节点(对应每个人)被信任的次数。`judge[i]` 表示第 `i` 个人被信任的次数。
111+
- 使用一个 `believer` 集合来记录所有信任了其他人的人。即,信任关系中的每个 `a` 都加入到 `believer` 中。
133112

113+
2. **处理信任关系:**
134114

135-
## 解题思路
115+
- 遍历 `trust` 数组,针对每个信任关系 `[a, b]`:
116+
-`judge[b]` 加 1,表示 `b` 被信任了一次。
117+
-`a` 加入到 `believer` 集合中,表示 `a` 信任了别人。
118+
119+
3. **寻找法官:**
120+
- 法官是一个特殊的个体,他被 `n - 1` 个人信任,并且他自己不信任任何人。因此,遍历 `judge` 数组,找到被信任次数等于 `n - 1` 且不在 `believer` 集合中的人。
121+
4. **返回结果:**
122+
- 如果找到符合条件的人,返回其编号(即法官编号);如果没有找到符合条件的人,则返回 `-1`
136123

137124
#### 复杂度分析
138125

139-
- **时间复杂度**:`O()`,
140-
- **空间复杂度**:`O()`,
126+
- **时间复杂度**:`O(t + n)`
127+
- 遍历 `trust` 数组的时间复杂度是 `O(T)`,其中 `t``trust` 数组的长度。
128+
- 遍历 `judge` 数组的时间复杂度是 `O(n)`,其中 `n` 是人数。
129+
- **空间复杂度**:`O(n)`
130+
- `judge` 数组的大小为 `n + 1`,占用 `O(n)` 的空间。
131+
- `believer` 集合最多包含 `n` 个人,占用 `O(n)` 的空间。
141132

142133
## 代码
143134

144135
```javascript
145-
136+
/**
137+
* @param {number} n
138+
* @param {number[][]} trust
139+
* @return {number}
140+
*/
141+
var findJudge = function (n, trust) {
142+
let judge = new Array(n + 1).fill(0),
143+
believer = new Set();
144+
145+
// 处理信任关系
146+
for (let [a, b] of trust) {
147+
judge[b]++;
148+
believer.add(a);
149+
}
150+
151+
// 寻找法官
152+
for (let i = 1; i <= n; i++) {
153+
if (judge[i] == n - 1 && !believer.has(i)) {
154+
return i;
155+
}
156+
}
157+
return -1;
158+
};
146159
```
147160

148161
## 相关题目

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /