diff --git a/lcci/03.04.Implement Queue using Stacks/README.md b/lcci/03.04.Implement Queue using Stacks/README.md index b20d26f7adcf1..d2b43370b5fb2 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..c9427f964e505 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