From 6a2d65b07bc5127bebefd53bb0f257f218e0c859 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 8 Jul 2024 09:11:09 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.071 --- .../README.md" | 38 +++++++++++++++++++ .../Solution.swift" | 33 ++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" index fc2a5d09cc0be..594fa816f1d18 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" @@ -222,6 +222,44 @@ func (this *Solution) PickIndex() int { */ ``` +#### Swift + +```swift +class Solution { + private var presum: [Int] + + init(_ w: [Int]) { + let n = w.count + presum = [Int](repeating: 0, count: n + 1) + for i in 0.. Int { + let n = presum.count + let x = Int.random(in: 1...presum[n - 1]) + var left = 0 + var right = n - 2 + while left < right { + let mid = (left + right) >> 1 + if presum[mid + 1] >= x { + right = mid + } else { + left = mid + 1 + } + } + return left + } +} +/** + * Your Solution object will be instantiated and called as such: + * let w = [1] + * let solution = Solution(w) + * solution.pickIndex() + */ +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.swift" new file mode 100644 index 0000000000000..3ae91801c5284 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.swift" @@ -0,0 +1,33 @@ +class Solution { + private var presum: [Int] + + init(_ w: [Int]) { + let n = w.count + presum = [Int](repeating: 0, count: n + 1) + for i in 0.. Int { + let n = presum.count + let x = Int.random(in: 1...presum[n - 1]) + var left = 0 + var right = n - 2 + while left < right { + let mid = (left + right) >> 1 + if presum[mid + 1] >= x { + right = mid + } else { + left = mid + 1 + } + } + return left + } +} +/** + * Your Solution object will be instantiated and called as such: + * let w = [1] + * let solution = Solution(w) + * solution.pickIndex() + */ \ No newline at end of file