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 6bea4aa

Browse files
feat: update lc problems (doocs#3697)
1 parent c310525 commit 6bea4aa

File tree

21 files changed

+239
-239
lines changed

21 files changed

+239
-239
lines changed

‎lcof2/剑指 Offer II 109. 开密码锁/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,12 @@ class Solution {
289289
if target == "0000" {
290290
return 0
291291
}
292-
292+
293293
var visited = Set<String>()
294294
var queue = ["0000"]
295295
visited.insert("0000")
296296
var step = 0
297-
297+
298298
while !queue.isEmpty {
299299
step += 1
300300
for _ in 0..<queue.count {
@@ -311,10 +311,10 @@ class Solution {
311311
}
312312
}
313313
}
314-
314+
315315
return -1
316316
}
317-
317+
318318
private func getNeighbors(_ lock: String) -> [String] {
319319
var neighbors = [String]()
320320
var chars = Array(lock)
@@ -328,7 +328,7 @@ class Solution {
328328
}
329329
return neighbors
330330
}
331-
331+
332332
private func prevChar(_ c: Character) -> Character {
333333
return c == "0" ? "9" : Character(UnicodeScalar(c.asciiValue! - 1))
334334
}

‎lcof2/剑指 Offer II 110. 所有路径/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Solution {
208208
results.append(Array(path))
209209
return
210210
}
211-
211+
212212
for next in graph[node] {
213213
path.append(next)
214214
dfs(next, &path)

‎lcof2/剑指 Offer II 111. 计算除法/README.md‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,23 @@ func find(x int) int {
298298
class Solution {
299299
private var parent = [Int]()
300300
private var weight = [Double]()
301-
301+
302302
func calcEquation(
303-
_ equations: [[String]],
304-
_ values: [Double],
303+
_ equations: [[String]],
304+
_ values: [Double],
305305
_ queries: [[String]]
306306
) -> [Double] {
307307
let n = equations.count
308308
parent = Array(0..<(n * 2))
309309
weight = Array(repeating: 1.0, count: n * 2)
310-
310+
311311
var map = [String: Int]()
312312
var index = 0
313-
313+
314314
for i in 0..<n {
315315
let a = equations[i][0]
316316
let b = equations[i][1]
317-
317+
318318
if map[a] == nil {
319319
map[a] = index
320320
index += 1
@@ -323,18 +323,18 @@ class Solution {
323323
map[b] = index
324324
index += 1
325325
}
326-
326+
327327
let pa = find(map[a]!)
328328
let pb = find(map[b]!)
329-
329+
330330
if pa != pb {
331331
parent[pa] = pb
332332
weight[pa] = weight[map[b]!] * values[i] / weight[map[a]!]
333333
}
334334
}
335-
335+
336336
var result = [Double]()
337-
337+
338338
for query in queries {
339339
let (c, d) = (query[0], query[1])
340340
if let id1 = map[c], let id2 = map[d], find(id1) == find(id2) {
@@ -343,10 +343,10 @@ class Solution {
343343
result.append(-1.0)
344344
}
345345
}
346-
346+
347347
return result
348348
}
349-
349+
350350
private func find(_ x: Int) -> Int {
351351
if parent[x] != x {
352352
let origin = parent[x]

‎lcof2/剑指 Offer II 112. 最长递增路径/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Solution {
219219
m = matrix.count
220220
n = matrix[0].count
221221
memo = Array(repeating: Array(repeating: -1, count: n), count: m)
222-
222+
223223
var ans = 0
224224
for i in 0..<m {
225225
for j in 0..<n {
@@ -235,7 +235,7 @@ class Solution {
235235
}
236236
var ans = 1
237237
let dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
238-
238+
239239
for (dx, dy) in dirs {
240240
let x = i + dx, y = j + dy
241241
if x >= 0, x < m, y >= 0, y < n, matrix[x][y] > matrix[i][j] {

‎lcof2/剑指 Offer II 113. 课程顺序/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,34 +276,34 @@ class Solution {
276276
func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {
277277
var graph = Array(repeating: [Int](), count: numCourses)
278278
var indegree = Array(repeating: 0, count: numCourses)
279-
279+
280280
for prereq in prerequisites {
281281
let course = prereq[0]
282282
let prereqCourse = prereq[1]
283283
graph[prereqCourse].append(course)
284284
indegree[course] += 1
285285
}
286-
286+
287287
var queue = [Int]()
288288
for i in 0..<numCourses {
289289
if indegree[i] == 0 {
290290
queue.append(i)
291291
}
292292
}
293-
293+
294294
var order = [Int]()
295295
while !queue.isEmpty {
296296
let course = queue.removeFirst()
297297
order.append(course)
298-
298+
299299
for nextCourse in graph[course] {
300300
indegree[nextCourse] -= 1
301301
if indegree[nextCourse] == 0 {
302302
queue.append(nextCourse)
303303
}
304304
}
305305
}
306-
306+
307307
return order.count == numCourses ? order : []
308308
}
309309
}

‎lcof2/剑指 Offer II 114. 外星文字典/README.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class Solution {
358358
var indegree = Array(repeating: 0, count: 26)
359359
var seen = Array(repeating: false, count: 26)
360360
var letterCount = 0
361-
361+
362362
for i in 0..<words.count - 1 {
363363
for char in words[i] {
364364
let index = Int(char.asciiValue! - Character("a").asciiValue!)
@@ -371,39 +371,39 @@ class Solution {
371371
for j in 0..<minLength {
372372
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
373373
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]
374-
374+
375375
if char1 != char2 {
376376
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
377377
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)
378-
378+
379379
if !graph[c1].contains(c2) {
380380
graph[c1].insert(c2)
381381
indegree[c2] += 1
382382
}
383383
break
384384
}
385-
385+
386386
if j == minLength - 1 && words[i].count > words[i + 1].count {
387387
return ""
388388
}
389389
}
390390
}
391-
391+
392392
for char in words[words.count - 1] {
393393
let index = Int(char.asciiValue! - Character("a").asciiValue!)
394394
if !seen[index] {
395395
seen[index] = true
396396
letterCount += 1
397397
}
398398
}
399-
399+
400400
var queue = [Int]()
401401
for i in 0..<26 {
402402
if seen[i] && indegree[i] == 0 {
403403
queue.append(i)
404404
}
405405
}
406-
406+
407407
var order = ""
408408
while !queue.isEmpty {
409409
let u = queue.removeFirst()
@@ -415,7 +415,7 @@ class Solution {
415415
}
416416
}
417417
}
418-
418+
419419
return order.count == letterCount ? order : ""
420420
}
421421
}

‎solution/2200-2299/2222.Number of Ways to Select Buildings/README.md‎

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一:统计 010 和 101 的出现次数
77+
### 方法一:计数 + 枚举
7878

79-
有效方案只有两种情况:010ドル$ 和 101ドル$。枚举中间数字,累计方案数
79+
根据题目描述,我们需要选择 3ドル$ 栋建筑,且相邻的两栋不能是同一类型
8080

81-
时间复杂度 $O(n),ドル其中 $n$ 表示 $s$ 的长度。
81+
我们可以枚举中间的建筑,假设为 $x,ドル那么左右两边的建筑类型只能是 $x \oplus 1,ドル其中 $\oplus$ 表示异或运算。因此,我们可以使用两个数组 $l$ 和 $r$ 分别记录左右两边的建筑类型的数量,然后枚举中间的建筑,计算答案即可。
82+
83+
时间复杂度 $O(n),ドル其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
8284

8385
<!-- tabs:start -->
8486

@@ -87,18 +89,13 @@ tags:
8789
```python
8890
class Solution:
8991
def numberOfWays(self, s: str) -> int:
90-
n = len(s)
91-
cnt0 = s.count("0")
92-
cnt1 = n - cnt0
93-
c0 = c1 = 0
92+
l = [0, 0]
93+
r = [s.count("0"), s.count("1")]
9494
ans = 0
95-
for c in s:
96-
if c == "0":
97-
ans += c1 * (cnt1 - c1)
98-
c0 += 1
99-
else:
100-
ans += c0 * (cnt0 - c0)
101-
c1 += 1
95+
for x in map(int, s):
96+
r[x] -= 1
97+
ans += l[x ^ 1] * r[x ^ 1]
98+
l[x] += 1
10299
return ans
103100
```
104101

@@ -108,23 +105,17 @@ class Solution:
108105
class Solution {
109106
public long numberOfWays(String s) {
110107
int n = s.length();
111-
int cnt0 = 0;
112-
for (char c : s.toCharArray()) {
113-
if (c == '0') {
114-
++cnt0;
115-
}
108+
int[] l = new int[2];
109+
int[] r = new int[2];
110+
for (int i = 0; i < n; ++i) {
111+
r[s.charAt(i) - '0']++;
116112
}
117-
int cnt1 = n - cnt0;
118113
long ans = 0;
119-
int c0 = 0, c1 = 0;
120-
for (char c : s.toCharArray()) {
121-
if (c == '0') {
122-
ans += c1 * (cnt1 - c1);
123-
++c0;
124-
} else {
125-
ans += c0 * (cnt0 - c0);
126-
++c1;
127-
}
114+
for (int i = 0; i < n; ++i) {
115+
int x = s.charAt(i) - '0';
116+
r[x]--;
117+
ans += 1L * l[x ^ 1] * r[x ^ 1];
118+
l[x]++;
128119
}
129120
return ans;
130121
}
@@ -138,19 +129,16 @@ class Solution {
138129
public:
139130
long long numberOfWays(string s) {
140131
int n = s.size();
141-
int cnt0 = 0;
142-
for (char& c : s) cnt0 += c == '0';
143-
int cnt1 = n - cnt0;
144-
int c0 = 0, c1 = 0;
132+
int l[2]{};
133+
int r[2]{};
134+
r[0] = ranges::count(s, '0');
135+
r[1] = n - r[0];
145136
long long ans = 0;
146-
for (char& c : s) {
147-
if (c == '0') {
148-
ans += c1 * (cnt1 - c1);
149-
++c0;
150-
} else {
151-
ans += c0 * (cnt0 - c0);
152-
++c1;
153-
}
137+
for (int i = 0; i < n; ++i) {
138+
int x = s[i] - '0';
139+
r[x]--;
140+
ans += 1LL * l[x ^ 1] * r[x ^ 1];
141+
l[x]++;
154142
}
155143
return ans;
156144
}
@@ -160,22 +148,38 @@ public:
160148
#### Go
161149
162150
```go
163-
func numberOfWays(s string) int64 {
151+
func numberOfWays(s string) (ans int64) {
164152
n := len(s)
165-
cnt0 := strings.Count(s, "0")
166-
cnt1 := n - cnt0
167-
c0, c1 := 0, 0
168-
ans := 0
153+
l := [2]int{}
154+
r := [2]int{}
155+
r[0] = strings.Count(s, "0")
156+
r[1] = n - r[0]
169157
for _, c := range s {
170-
if c == '0' {
171-
ans += c1 * (cnt1 - c1)
172-
c0++
173-
} else {
174-
ans += c0 * (cnt0 - c0)
175-
c1++
176-
}
158+
x := int(c - '0')
159+
r[x]--
160+
ans += int64(l[x^1] * r[x^1])
161+
l[x]++
177162
}
178-
return int64(ans)
163+
return
164+
}
165+
```
166+
167+
#### TypeScript
168+
169+
```ts
170+
function numberOfWays(s: string): number {
171+
const n = s.length;
172+
const l: number[] = [0, 0];
173+
const r: number[] = [s.split('').filter(c => c === '0').length, 0];
174+
r[1] = n - r[0];
175+
let ans: number = 0;
176+
for (const c of s) {
177+
const x = c === '0' ? 0 : 1;
178+
r[x]--;
179+
ans += l[x ^ 1] * r[x ^ 1];
180+
l[x]++;
181+
}
182+
return ans;
179183
}
180184
```
181185

0 commit comments

Comments
(0)

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