Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lcof2/剑指 Offer II 106. 二分图/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@ func isBipartite(graph [][]int) bool {
}
```

#### Swift

```swift
class Solution {
private var parent: [Int] = []

func isBipartite(_ graph: [[Int]]) -> Bool {
let n = graph.count
parent = Array(0..<n)

for u in 0..<n {
for v in graph[u] {
if find(u) == find(v) {
return false
}
parent[find(v)] = find(graph[u][0])
}
}
return true
}

private func find(_ x: Int) -> Int {
if parent[x] != x {
parent[x] = find(parent[x])
}
return parent[x]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions lcof2/剑指 Offer II 106. 二分图/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
private var parent: [Int] = []

func isBipartite(_ graph: [[Int]]) -> Bool {
let n = graph.count
parent = Array(0..<n)

for u in 0..<n {
for v in graph[u] {
if find(u) == find(v) {
return false
}
parent[find(v)] = find(graph[u][0])
}
}
return true
}

private func find(_ x: Int) -> Int {
if parent[x] != x {
parent[x] = find(parent[x])
}
return parent[x]
}
}
Loading