From b422a54c8d2b4307da9d2820632b3db27f6e69af Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 22 Apr 2024 13:17:31 +0100 Subject: [PATCH 1/2] Swift Implementation for LCCI 03.04 --- .../README.md | 46 +++++++++++++++++++ .../README_EN.md | 46 +++++++++++++++++++ .../Solution.swift | 43 +++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 lcci/03.04.Implement Queue using Stacks/Solution.swift diff --git a/lcci/03.04.Implement Queue using Stacks/README.md b/lcci/03.04.Implement Queue using Stacks/README.md index b20d26f7adcf1..ec5c050835c76 100644 --- a/lcci/03.04.Implement Queue using Stacks/README.md +++ b/lcci/03.04.Implement Queue using Stacks/README.md @@ -299,6 +299,52 @@ impl MyQueue { */ ``` +```swift +class MyQueue { + private var stk1: [Int] = [] + private var stk2: [Int] = [] + + init() {} + + func push(_ x: Int) { + stk1.append(x) + } + + @discardableResult + func pop() -> Int { + move() + return stk2.removeLast() + } + + func peek() -> Int { + move() + return stk2.last! + } + + func empty() -> Bool { + return stk1.isEmpty && stk2.isEmpty + } + + private func move() { + if stk2.isEmpty { + while !stk1.isEmpty { + stk2.append(stk1.removeLast()) + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * let obj = new MyQueue(); + * obj.push(x); + * let param_2 = obj.pop(); + * let param_3 = obj.peek(); + * var myValue : Bool + * myValue = obj.empty(); + */ +``` + diff --git a/lcci/03.04.Implement Queue using Stacks/README_EN.md b/lcci/03.04.Implement Queue using Stacks/README_EN.md index 78b7e19cf7331..8a95f34b2e55b 100644 --- a/lcci/03.04.Implement Queue using Stacks/README_EN.md +++ b/lcci/03.04.Implement Queue using Stacks/README_EN.md @@ -330,6 +330,52 @@ impl MyQueue { */ ``` +```swift +class MyQueue { + private var stk1: [Int] = [] + private var stk2: [Int] = [] + + init() {} + + func push(_ x: Int) { + stk1.append(x) + } + + @discardableResult + func pop() -> Int { + move() + return stk2.removeLast() + } + + func peek() -> Int { + move() + return stk2.last! + } + + func empty() -> Bool { + return stk1.isEmpty && stk2.isEmpty + } + + private func move() { + if stk2.isEmpty { + while !stk1.isEmpty { + stk2.append(stk1.removeLast()) + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * let obj = new MyQueue(); + * obj.push(x); + * let param_2 = obj.pop(); + * let param_3 = obj.peek(); + * var myValue : Bool + * myValue = obj.empty(); + */ +``` + diff --git a/lcci/03.04.Implement Queue using Stacks/Solution.swift b/lcci/03.04.Implement Queue using Stacks/Solution.swift new file mode 100644 index 0000000000000..e2aafc5b74060 --- /dev/null +++ b/lcci/03.04.Implement Queue using Stacks/Solution.swift @@ -0,0 +1,43 @@ +class MyQueue { + private var stk1: [Int] = [] + private var stk2: [Int] = [] + + init() {} + + func push(_ x: Int) { + stk1.append(x) + } + + @discardableResult + func pop() -> Int { + move() + return stk2.removeLast() + } + + func peek() -> Int { + move() + return stk2.last! + } + + func empty() -> Bool { + return stk1.isEmpty && stk2.isEmpty + } + + private func move() { + if stk2.isEmpty { + while !stk1.isEmpty { + stk2.append(stk1.removeLast()) + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * let obj = new MyQueue(); + * obj.push(x); + * let param_2 = obj.pop(); + * let param_3 = obj.peek(); + * var myValue : Bool + * myValue = obj.empty(); + */ \ No newline at end of file From 3b7f10d731d2bc70686227984a3b6a8b3c01cbcd Mon Sep 17 00:00:00 2001 From: klever34 Date: Mon, 22 Apr 2024 12:32:02 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/03.04.Implement Queue using Stacks/README.md | 2 +- lcci/03.04.Implement Queue using Stacks/README_EN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lcci/03.04.Implement Queue using Stacks/README.md b/lcci/03.04.Implement Queue using Stacks/README.md index ec5c050835c76..d2b43370b5fb2 100644 --- a/lcci/03.04.Implement Queue using Stacks/README.md +++ b/lcci/03.04.Implement Queue using Stacks/README.md @@ -340,7 +340,7 @@ class MyQueue { * obj.push(x); * let param_2 = obj.pop(); * let param_3 = obj.peek(); - * var myValue : Bool + * var myValue : Bool * myValue = obj.empty(); */ ``` diff --git a/lcci/03.04.Implement Queue using Stacks/README_EN.md b/lcci/03.04.Implement Queue using Stacks/README_EN.md index 8a95f34b2e55b..c9427f964e505 100644 --- a/lcci/03.04.Implement Queue using Stacks/README_EN.md +++ b/lcci/03.04.Implement Queue using Stacks/README_EN.md @@ -371,7 +371,7 @@ class MyQueue { * obj.push(x); * let param_2 = obj.pop(); * let param_3 = obj.peek(); - * var myValue : Bool + * var myValue : Bool * myValue = obj.empty(); */ ```