From e6146e455f3f98dbdf1a0239840245f975e83be0 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Sun, 30 Jun 2024 17:37:36 +0100 Subject: [PATCH] feat: add swift implementation to lcof2 problem: No.066 --- .../README.md" | 28 +++++++++++++++++++ .../Solution.swift" | 23 +++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.swift" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" index ac892f1b46e05..d09317828a0c3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" @@ -196,6 +196,34 @@ func (this *MapSum) Sum(prefix string) int { */ ``` +#### Swift + +```swift +class MapSum { + private var data: [String: Int] + private var t: [String: Int] + + init() { + data = [String: Int]() + t = [String: Int]() + } + + func insert(_ key: String, _ val: Int) { + let old = t[key] ?? 0 + t[key] = val + for i in 1...key.count { + let endIndex = key.index(key.startIndex, offsetBy: i) + let k = String(key[key.startIndex.. Int { + return data[prefix] ?? 0 + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.swift" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.swift" new file mode 100644 index 0000000000000..f095026cf16da --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.swift" @@ -0,0 +1,23 @@ +class MapSum { + private var data: [String: Int] + private var t: [String: Int] + + init() { + data = [String: Int]() + t = [String: Int]() + } + + func insert(_ key: String, _ val: Int) { + let old = t[key] ?? 0 + t[key] = val + for i in 1...key.count { + let endIndex = key.index(key.startIndex, offsetBy: i) + let k = String(key[key.startIndex.. Int { + return data[prefix] ?? 0 + } +} \ No newline at end of file