diff --git a/lcci/03.03.Stack of Plates/README.md b/lcci/03.03.Stack of Plates/README.md index 637bd3f17986f..c51074ebb33bd 100644 --- a/lcci/03.03.Stack of Plates/README.md +++ b/lcci/03.03.Stack of Plates/README.md @@ -258,6 +258,51 @@ class StackOfPlates { */ ``` +```swift +class StackOfPlates { + private var stacks: [[Int]] + private var cap: Int + + init(_ cap: Int) { + self.cap = cap + self.stacks = [] + } + + func push(_ val: Int) { + if cap == 0 { + return + } + if stacks.isEmpty || stacks.last!.count >= cap { + stacks.append([]) + } + stacks[stacks.count - 1].append(val) + } + + func pop() -> Int { + return popAt(stacks.count - 1) + } + + func popAt(_ index: Int) -> Int { + guard index >= 0, index < stacks.count, !stacks[index].isEmpty else { + return -1 + } + let value = stacks[index].removeLast() + if stacks[index].isEmpty { + stacks.remove(at: index) + } + return value + } +} + +/** + * Your StackOfPlates object will be instantiated and called as such: + * let obj = new StackOfPlates(cap); + * obj.push(val); + * let param_2 = obj.pop(); + * let param_3 = obj.popAt(index); + */ +``` + diff --git a/lcci/03.03.Stack of Plates/README_EN.md b/lcci/03.03.Stack of Plates/README_EN.md index eaa9e0eabdc1d..7b464bacc1008 100644 --- a/lcci/03.03.Stack of Plates/README_EN.md +++ b/lcci/03.03.Stack of Plates/README_EN.md @@ -273,6 +273,51 @@ class StackOfPlates { */ ``` +```swift +class StackOfPlates { + private var stacks: [[Int]] + private var cap: Int + + init(_ cap: Int) { + self.cap = cap + self.stacks = [] + } + + func push(_ val: Int) { + if cap == 0 { + return + } + if stacks.isEmpty || stacks.last!.count >= cap { + stacks.append([]) + } + stacks[stacks.count - 1].append(val) + } + + func pop() -> Int { + return popAt(stacks.count - 1) + } + + func popAt(_ index: Int) -> Int { + guard index >= 0, index < stacks.count, !stacks[index].isEmpty else { + return -1 + } + let value = stacks[index].removeLast() + if stacks[index].isEmpty { + stacks.remove(at: index) + } + return value + } +} + +/** + * Your StackOfPlates object will be instantiated and called as such: + * let obj = new StackOfPlates(cap); + * obj.push(val); + * let param_2 = obj.pop(); + * let param_3 = obj.popAt(index); + */ +``` + diff --git a/lcci/03.03.Stack of Plates/Solution.swift b/lcci/03.03.Stack of Plates/Solution.swift new file mode 100644 index 0000000000000..6b1c7f4aa7065 --- /dev/null +++ b/lcci/03.03.Stack of Plates/Solution.swift @@ -0,0 +1,42 @@ +class StackOfPlates { + private var stacks: [[Int]] + private var cap: Int + + init(_ cap: Int) { + self.cap = cap + self.stacks = [] + } + + func push(_ val: Int) { + if cap == 0 { + return + } + if stacks.isEmpty || stacks.last!.count >= cap { + stacks.append([]) + } + stacks[stacks.count - 1].append(val) + } + + func pop() -> Int { + return popAt(stacks.count - 1) + } + + func popAt(_ index: Int) -> Int { + guard index >= 0, index < stacks.count, !stacks[index].isEmpty else { + return -1 + } + let value = stacks[index].removeLast() + if stacks[index].isEmpty { + stacks.remove(at: index) + } + return value + } +} + +/** + * Your StackOfPlates object will be instantiated and called as such: + * let obj = new StackOfPlates(cap); + * obj.push(val); + * let param_2 = obj.pop(); + * let param_3 = obj.popAt(index); + */ \ No newline at end of file