Skip to content

Commit a71d956

Browse files
authored
Merge branch 'main' into patch-1
2 parents 34b6e73 + 32c0fd2 commit a71d956

File tree

199 files changed

+34541
-26887
lines changed

Some content is hidden

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

199 files changed

+34541
-26887
lines changed

images/starcharts.svg

Lines changed: 25730 additions & 25630 deletions
Loading

lcof/面试题24. 反转链表/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ class Solution {
275275
func reverseList(_ head: ListNode?) -> ListNode? {
276276
let dummy = ListNode(0)
277277
var curr = head
278-
278+
279279
while curr != nil {
280280
let next = curr?.next
281281
curr?.next = dummy.next
282282
dummy.next = curr
283283
curr = next
284284
}
285-
285+
286286
return dummy.next
287287
}
288288
}

lcof/面试题25. 合并两个排序的链表/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class Solution {
330330
var cur: ListNode? = dummy
331331
var l1 = l1
332332
var l2 = l2
333-
333+
334334
while let l1Node = l1, let l2Node = l2 {
335335
if l1Node.val <= l2Node.val {
336336
cur?.next = l1Node
@@ -341,9 +341,9 @@ class Solution {
341341
}
342342
cur = cur?.next
343343
}
344-
344+
345345
cur?.next = l1 ?? l2
346-
346+
347347
return dummy.next
348348
}
349349
}

lcof/面试题29. 顺时针打印矩阵/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,14 @@ class Solution {
334334
guard !matrix.isEmpty && !matrix[0].isEmpty else {
335335
return []
336336
}
337-
337+
338338
let m = matrix.count
339339
let n = matrix[0].count
340340
var vis = Array(repeating: Array(repeating: false, count: n), count: m)
341341
var ans = [Int]()
342342
var i = 0, j = 0, k = 0
343343
let dirs = [0, 1, 0, -1, 0]
344-
344+
345345
for _ in 0..<m*n {
346346
ans.append(matrix[i][j])
347347
vis[i][j] = true
@@ -354,7 +354,7 @@ class Solution {
354354
i = x
355355
j = y
356356
}
357-
357+
358358
return ans
359359
}
360360
}

lcof/面试题31. 栈的压入、弹出序列/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ class Solution {
215215
func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool {
216216
var stack = [Int]()
217217
var j = 0
218-
218+
219219
for v in pushed {
220220
stack.append(v)
221221
while !stack.isEmpty && stack.last == popped[j] {
222222
stack.removeLast()
223223
j += 1
224224
}
225225
}
226-
226+
227227
return j == pushed.count
228228
}
229229
}

lcof/面试题34. 二叉树中和为某一值的路径/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public class Solution {
399399
* }
400400
* }
401401
*/
402-
402+
403403
class Solution {
404404
private var t = [Int]()
405405
private var ans = [[Int]]()

lcof/面试题35. 复杂链表的复制/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,17 @@ public class Solution {
296296
* public var val: Int
297297
* public var next: Node?
298298
* public var random: Node?
299-
299+
300300
* public init(_ val: Int) {
301301
* self.val = val
302302
* self.next = nil
303303
* self.random = nil
304304
* }
305-
305+
306306
* public static func == (lhs: Node, rhs: Node) -> Bool {
307307
* return lhs === rhs
308308
* }
309-
309+
310310
* public func hash(into hasher: inout Hasher) {
311311
* hasher.combine(ObjectIdentifier(self))
312312
* }
@@ -319,23 +319,23 @@ class Solution {
319319
let dummy = Node(0)
320320
var tail: Node? = dummy
321321
var cur = head
322-
322+
323323
while cur != nil {
324324
tail?.next = Node(cur!.val)
325325
tail = tail?.next
326326
d[cur!] = tail
327327
cur = cur?.next
328328
}
329-
329+
330330
tail = dummy.next
331331
cur = head
332-
332+
333333
while cur != nil {
334334
tail?.random = d[cur!.random ?? Node(0)]
335335
tail = tail?.next
336336
cur = cur?.next
337337
}
338-
338+
339339
return dummy.next
340340
}
341341
}

lcof/面试题38. 字符串的排列/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,43 @@ public class Solution {
283283
}
284284
```
285285

286+
#### Swift
287+
288+
```swift
289+
class Solution {
290+
private var ans: [String] = []
291+
private var cs: [Character] = []
292+
293+
func permutation(_ s: String) -> [String] {
294+
cs = Array(s)
295+
dfs(0)
296+
return ans
297+
}
298+
299+
private func dfs(_ i: Int) {
300+
if i == cs.count - 1 {
301+
ans.append(String(cs))
302+
return
303+
}
304+
var vis: Set<Character> = []
305+
for j in i..<cs.count {
306+
if !vis.contains(cs[j]) {
307+
vis.insert(cs[j])
308+
swap(i, j)
309+
dfs(i + 1)
310+
swap(i, j)
311+
}
312+
}
313+
}
314+
315+
private func swap(_ i: Int, _ j: Int) {
316+
let t = cs[i]
317+
cs[i] = cs[j]
318+
cs[j] = t
319+
}
320+
}
321+
```
322+
286323
<!-- tabs:end -->
287324

288325
<!-- solution:end -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
private var ans: [String] = []
3+
private var cs: [Character] = []
4+
5+
func permutation(_ s: String) -> [String] {
6+
cs = Array(s)
7+
dfs(0)
8+
return ans
9+
}
10+
11+
private func dfs(_ i: Int) {
12+
if i == cs.count - 1 {
13+
ans.append(String(cs))
14+
return
15+
}
16+
var vis: Set<Character> = []
17+
for j in i..<cs.count {
18+
if !vis.contains(cs[j]) {
19+
vis.insert(cs[j])
20+
swap(i, j)
21+
dfs(i + 1)
22+
swap(i, j)
23+
}
24+
}
25+
}
26+
27+
private func swap(_ i: Int, _ j: Int) {
28+
let t = cs[i]
29+
cs[i] = cs[j]
30+
cs[j] = t
31+
}
32+
}

lcof/面试题39. 数组中出现次数超过一半的数字/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,27 @@ public class Solution {
196196
}
197197
```
198198

199+
#### Swift
200+
201+
```swift
202+
class Solution {
203+
func majorityElement(_ nums: [Int]) -> Int {
204+
var cnt = 0
205+
var m = 0
206+
207+
for v in nums {
208+
if cnt == 0 {
209+
m = v
210+
cnt = 1
211+
} else {
212+
cnt += (m == v ? 1 : -1)
213+
}
214+
}
215+
return m
216+
}
217+
}
218+
```
219+
199220
<!-- tabs:end -->
200221

201222
<!-- solution:end -->

0 commit comments

Comments
 (0)