diff --git a/lcci/03.01.Three in One/README.md b/lcci/03.01.Three in One/README.md index 4ba39f5bba2f3..4c467e926ee85 100644 --- a/lcci/03.01.Three in One/README.md +++ b/lcci/03.01.Three in One/README.md @@ -266,6 +266,53 @@ class TripleInOne { */ ``` +```swift +class TripleInOne { + private var cap: Int + private var stk: [Int] + + init(_ stackSize: Int) { + self.cap = stackSize + self.stk = [Int](repeating: 0, count: cap * 3 + 3) + } + + func push(_ stackNum: Int, _ value: Int) { + if stk[cap * 3 + stackNum] < cap { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value + stk[cap * 3 + stackNum] += 1 + } + } + + func pop(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + stk[cap * 3 + stackNum] -= 1 + return stk[cap * stackNum + stk[cap * 3 + stackNum]] + } + + func peek(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1] + } + + func isEmpty(_ stackNum: Int) -> Bool { + return stk[cap * 3 + stackNum] == 0 + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * let obj = TripleInOne(stackSize) + * obj.push(stackNum,value) + * let param_2 = obj.pop(stackNum) + * let param_3 = obj.peek(stackNum) + * let param_4 = obj.isEmpty(stackNum) + */ +``` + diff --git a/lcci/03.01.Three in One/README_EN.md b/lcci/03.01.Three in One/README_EN.md index a1be1d9b41eda..62d02bde904b0 100644 --- a/lcci/03.01.Three in One/README_EN.md +++ b/lcci/03.01.Three in One/README_EN.md @@ -280,6 +280,53 @@ class TripleInOne { */ ``` +```swift +class TripleInOne { + private var cap: Int + private var stk: [Int] + + init(_ stackSize: Int) { + self.cap = stackSize + self.stk = [Int](repeating: 0, count: cap * 3 + 3) + } + + func push(_ stackNum: Int, _ value: Int) { + if stk[cap * 3 + stackNum] < cap { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value + stk[cap * 3 + stackNum] += 1 + } + } + + func pop(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + stk[cap * 3 + stackNum] -= 1 + return stk[cap * stackNum + stk[cap * 3 + stackNum]] + } + + func peek(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1] + } + + func isEmpty(_ stackNum: Int) -> Bool { + return stk[cap * 3 + stackNum] == 0 + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * let obj = TripleInOne(stackSize) + * obj.push(stackNum,value) + * let param_2 = obj.pop(stackNum) + * let param_3 = obj.peek(stackNum) + * let param_4 = obj.isEmpty(stackNum) + */ +``` + diff --git a/lcci/03.01.Three in One/Solution.swift b/lcci/03.01.Three in One/Solution.swift new file mode 100644 index 0000000000000..04dd229bb6c98 --- /dev/null +++ b/lcci/03.01.Three in One/Solution.swift @@ -0,0 +1,44 @@ +class TripleInOne { + private var cap: Int + private var stk: [Int] + + init(_ stackSize: Int) { + self.cap = stackSize + self.stk = [Int](repeating: 0, count: cap * 3 + 3) + } + + func push(_ stackNum: Int, _ value: Int) { + if stk[cap * 3 + stackNum] < cap { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value + stk[cap * 3 + stackNum] += 1 + } + } + + func pop(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + stk[cap * 3 + stackNum] -= 1 + return stk[cap * stackNum + stk[cap * 3 + stackNum]] + } + + func peek(_ stackNum: Int) -> Int { + if isEmpty(stackNum) { + return -1 + } + return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1] + } + + func isEmpty(_ stackNum: Int) -> Bool { + return stk[cap * 3 + stackNum] == 0 + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * let obj = TripleInOne(stackSize) + * obj.push(stackNum,value) + * let param_2 = obj.pop(stackNum) + * let param_3 = obj.peek(stackNum) + * let param_4 = obj.isEmpty(stackNum) + */ \ No newline at end of file