From dfced6f6f74672ba81a632d4cf5a53706c83ef15 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 8 Oct 2024 07:59:49 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.106 --- .../README.md" | 30 +++++++++++++++++++ .../Solution.swift" | 25 ++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" index 710a738b34294..b0afc43b343c1 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" @@ -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.. Int { + if parent[x] != x { + parent[x] = find(parent[x]) + } + return parent[x] + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.swift" new file mode 100644 index 0000000000000..9ebd6dd7071b7 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.swift" @@ -0,0 +1,25 @@ +class Solution { + private var parent: [Int] = [] + + func isBipartite(_ graph: [[Int]]) -> Bool { + let n = graph.count + parent = Array(0.. Int { + if parent[x] != x { + parent[x] = find(parent[x]) + } + return parent[x] + } +} \ No newline at end of file