From 3699fc88ce16a127e295aa490eae041b55f4d8d0 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 20 May 2024 07:49:36 +0100 Subject: [PATCH 1/2] Swift Implementation for LCOF 09 --- .../README.md" | 30 +++++++++++++++++++ .../Solution.swift" | 20 +++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index be00711a0bbf9..af47e765d28f5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -336,6 +336,36 @@ public class CQueue { */ ``` +```swift +class CQueue { + private var stk1: [Int] = [] + private var stk2: [Int] = [] + + init() { + } + + func appendTail(_ value: Int) { + stk1.append(value) + } + + func deleteHead() -> Int { + if stk2.isEmpty { + while !stk1.isEmpty { + stk2.append(stk1.removeLast()) + } + } + return stk2.isEmpty ? -1 : stk2.removeLast() + } +} + +/** + * Your CQueue object will be instantiated and called as such: + * let obj = CQueue(); + * obj.appendTail(value); + * let param_2 = obj.DeleteHead(); + */ +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.swift" new file mode 100644 index 0000000000000..9d6664ec61f43 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.swift" @@ -0,0 +1,20 @@ +class CQueue { + private var stk1: [Int] = [] + private var stk2: [Int] = [] + + init() { + } + + func appendTail(_ value: Int) { + stk1.append(value) + } + + func deleteHead() -> Int { + if stk2.isEmpty { + while !stk1.isEmpty { + stk2.append(stk1.removeLast()) + } + } + return stk2.isEmpty ? -1 : stk2.removeLast() + } +} \ No newline at end of file From 6537570a54fdd0f4bd23355caf6578866bd95c6b Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 20 May 2024 08:58:51 +0100 Subject: [PATCH 2/2] code update --- .../README.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index af47e765d28f5..696d6fbb87275 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -336,6 +336,8 @@ public class CQueue { */ ``` +#### Swift + ```swift class CQueue { private var stk1: [Int] = []