From f8d0c317432bac78e262d7605e145ecef6ce307b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 3 Apr 2024 10:00:43 +0800 Subject: [PATCH 1/2] feat: add solutions to lcci problem: No.03.01 No.03.01.Three in One --- lcci/03.01.Three in One/README.md | 188 ++++++++++++++++++++------ lcci/03.01.Three in One/README_EN.md | 188 ++++++++++++++++++++------ lcci/03.01.Three in One/Solution.cpp | 43 ++++++ lcci/03.01.Three in One/Solution.go | 38 ++---- lcci/03.01.Three in One/Solution.java | 30 ++-- lcci/03.01.Three in One/Solution.py | 21 ++- lcci/03.01.Three in One/Solution.ts | 44 ++++++ 7 files changed, 414 insertions(+), 138 deletions(-) create mode 100644 lcci/03.01.Three in One/Solution.cpp create mode 100644 lcci/03.01.Three in One/Solution.ts diff --git a/lcci/03.01.Three in One/README.md b/lcci/03.01.Three in One/README.md index 32a5e66fbc233..4ba39f5bba2f3 100644 --- a/lcci/03.01.Three in One/README.md +++ b/lcci/03.01.Three in One/README.md @@ -32,28 +32,47 @@ ## 解法 -### 方法一 +### 方法一:数组模拟 + +我们使用一个变量 $cap$ 来表示每个栈的大小,使用一个长度为 $3 \times \text{cap} + 3$ 的数组 $stk$ 来模拟三个栈,数组的前 $3 \times \text{cap}$ 个元素用来存储栈的元素,数组的后三个元素用来存储每个栈的元素个数。 + +对于 `push` 操作,我们首先判断栈是否已满,如果未满,则将元素压入栈中,并更新栈的元素个数。 + +对于 `pop` 操作,我们首先判断栈是否为空,如果不为空,则更新栈的元素个数,并返回栈顶元素。 + +对于 `peek` 操作,我们首先判断栈是否为空,如果不为空,则返回栈顶元素。 + +对于 `isEmpty` 操作,我们直接判断栈是否为空即可。对于栈 $i$,我们只需要判断 $stk[\text{cap} \times 3 + i]$ 是否为 $0$ 即可。 + +时间复杂度上,每个操作的时间复杂度均为 $O(1)$。空间复杂度为 $O(\text{cap})$,其中 $\text{cap}$ 为栈的大小。 ```python class TripleInOne: + def __init__(self, stackSize: int): - self._capacity = stackSize - self._s = [[], [], []] + self.cap = stackSize + self.stk = [0] * (self.cap * 3 + 3) def push(self, stackNum: int, value: int) -> None: - if len(self._s[stackNum]) < self._capacity: - self._s[stackNum].append(value) + if self.stk[self.cap * 3 + stackNum] < self.cap: + self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] = value + self.stk[self.cap * 3 + stackNum] += 1 def pop(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop() + if self.isEmpty(stackNum): + return -1 + self.stk[self.cap * 3 + stackNum] -= 1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] def peek(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1] + if self.isEmpty(stackNum): + return -1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum] - 1] def isEmpty(self, stackNum: int) -> bool: - return len(self._s[stackNum]) == 0 + return self.stk[self.cap * 3 + stackNum] == 0 # Your TripleInOne object will be instantiated and called as such: @@ -66,18 +85,18 @@ class TripleInOne: ```java class TripleInOne { - private int[] s; - private int capacity; + private int cap; + private int[] stk; public TripleInOne(int stackSize) { - s = new int[stackSize * 3 + 3]; - capacity = stackSize; + cap = stackSize; + stk = new int[cap * 3 + 3]; } public void push(int stackNum, int value) { - if (s[stackNum + 3 * capacity] < capacity) { - s[s[stackNum + 3 * capacity] * 3 + stackNum] = value; - ++s[stackNum + 3 * capacity]; + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; } } @@ -85,16 +104,16 @@ class TripleInOne { if (isEmpty(stackNum)) { return -1; } - --s[stackNum + 3 * capacity]; - return s[s[stackNum + 3 * capacity] * 3 + stackNum]; + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; } public int peek(int stackNum) { - return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum]; + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; } public boolean isEmpty(int stackNum) { - return s[stackNum + 3 * capacity] == 0; + return stk[cap * 3 + stackNum] == 0; } } @@ -108,54 +127,86 @@ class TripleInOne { */ ``` +```cpp +class TripleInOne { +public: + TripleInOne(int stackSize) { + cap = stackSize; + stk.resize(cap * 3 + 3); + } + + void push(int stackNum, int value) { + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; + } + } + + int pop(int stackNum) { + if (isEmpty(stackNum)) { + return -1; + } + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; + } + + int peek(int stackNum) { + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; + } + + bool isEmpty(int stackNum) { + return stk[cap * 3 + stackNum] == 0; + } + +private: + int cap; + vector stk; +}; + +/** + * Your TripleInOne object will be instantiated and called as such: + * TripleInOne* obj = new TripleInOne(stackSize); + * obj->push(stackNum,value); + * int param_2 = obj->pop(stackNum); + * int param_3 = obj->peek(stackNum); + * bool param_4 = obj->isEmpty(stackNum); + */ +``` + ```go type TripleInOne struct { - data []int - offset [3]int - stackSize int + cap int + stk []int } func Constructor(stackSize int) TripleInOne { - total := stackSize * 3 - data := make([]int, total) - offset := [3]int{} - for i := 0; i < 3; i++ { - offset[i] = i * stackSize - } - return TripleInOne{ - data: data, - offset: offset, - stackSize: stackSize, - } + return TripleInOne{stackSize, make([]int, stackSize*3+3)} } func (this *TripleInOne) Push(stackNum int, value int) { - i := this.offset[stackNum] - if i < (stackNum+1)*this.stackSize { - this.data[i] = value - this.offset[stackNum]++ + if this.stk[this.cap*3+stackNum] < this.cap { + this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] = value + this.stk[this.cap*3+stackNum]++ } } func (this *TripleInOne) Pop(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - this.offset[stackNum]-- - return this.data[i-1] + this.stk[this.cap*3+stackNum]-- + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] } func (this *TripleInOne) Peek(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - return this.data[i-1] + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]-1] } func (this *TripleInOne) IsEmpty(stackNum int) bool { - return this.offset[stackNum] == stackNum*this.stackSize + return this.stk[this.cap*3+stackNum] == 0 } /** @@ -168,6 +219,53 @@ func (this *TripleInOne) IsEmpty(stackNum int) bool { */ ``` +```ts +class TripleInOne { + private cap: number; + private stk: number[]; + + constructor(stackSize: number) { + this.cap = stackSize; + this.stk = Array(stackSize * 3 + 3).fill(0); + } + + push(stackNum: number, value: number): void { + if (this.stk[this.cap * 3 + stackNum] < this.cap) { + this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]] = value; + this.stk[this.cap * 3 + stackNum]++; + } + } + + pop(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + this.stk[this.cap * 3 + stackNum]--; + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]]; + } + + peek(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum] - 1]; + } + + isEmpty(stackNum: number): boolean { + return this.stk[this.cap * 3 + stackNum] === 0; + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * var obj = new TripleInOne(stackSize) + * obj.push(stackNum,value) + * var param_2 = obj.pop(stackNum) + * var param_3 = obj.peek(stackNum) + * var 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 ec7d209a4dbab..a1be1d9b41eda 100644 --- a/lcci/03.01.Three in One/README_EN.md +++ b/lcci/03.01.Three in One/README_EN.md @@ -46,28 +46,47 @@ ## Solutions -### Solution 1 +### Solution 1: Array Simulation + +We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \text{cap} + 3$ to simulate three stacks. The first $3 \times \text{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack. + +For the `push` operation, we first check whether the stack is full. If it is not full, we push the element into the stack and update the number of elements in the stack. + +For the `pop` operation, we first check whether the stack is empty. If it is not empty, we update the number of elements in the stack and return the top element of the stack. + +For the `peek` operation, we first check whether the stack is empty. If it is not empty, we return the top element of the stack. + +For the `isEmpty` operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\text{cap} \times 3 + i]$ is $0$. + +In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\text{cap})$, where $\text{cap}$ is the size of the stack. ```python class TripleInOne: + def __init__(self, stackSize: int): - self._capacity = stackSize - self._s = [[], [], []] + self.cap = stackSize + self.stk = [0] * (self.cap * 3 + 3) def push(self, stackNum: int, value: int) -> None: - if len(self._s[stackNum]) < self._capacity: - self._s[stackNum].append(value) + if self.stk[self.cap * 3 + stackNum] < self.cap: + self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] = value + self.stk[self.cap * 3 + stackNum] += 1 def pop(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop() + if self.isEmpty(stackNum): + return -1 + self.stk[self.cap * 3 + stackNum] -= 1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] def peek(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1] + if self.isEmpty(stackNum): + return -1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum] - 1] def isEmpty(self, stackNum: int) -> bool: - return len(self._s[stackNum]) == 0 + return self.stk[self.cap * 3 + stackNum] == 0 # Your TripleInOne object will be instantiated and called as such: @@ -80,18 +99,18 @@ class TripleInOne: ```java class TripleInOne { - private int[] s; - private int capacity; + private int cap; + private int[] stk; public TripleInOne(int stackSize) { - s = new int[stackSize * 3 + 3]; - capacity = stackSize; + cap = stackSize; + stk = new int[cap * 3 + 3]; } public void push(int stackNum, int value) { - if (s[stackNum + 3 * capacity] < capacity) { - s[s[stackNum + 3 * capacity] * 3 + stackNum] = value; - ++s[stackNum + 3 * capacity]; + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; } } @@ -99,16 +118,16 @@ class TripleInOne { if (isEmpty(stackNum)) { return -1; } - --s[stackNum + 3 * capacity]; - return s[s[stackNum + 3 * capacity] * 3 + stackNum]; + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; } public int peek(int stackNum) { - return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum]; + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; } public boolean isEmpty(int stackNum) { - return s[stackNum + 3 * capacity] == 0; + return stk[cap * 3 + stackNum] == 0; } } @@ -122,54 +141,86 @@ class TripleInOne { */ ``` +```cpp +class TripleInOne { +public: + TripleInOne(int stackSize) { + cap = stackSize; + stk.resize(cap * 3 + 3); + } + + void push(int stackNum, int value) { + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; + } + } + + int pop(int stackNum) { + if (isEmpty(stackNum)) { + return -1; + } + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; + } + + int peek(int stackNum) { + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; + } + + bool isEmpty(int stackNum) { + return stk[cap * 3 + stackNum] == 0; + } + +private: + int cap; + vector stk; +}; + +/** + * Your TripleInOne object will be instantiated and called as such: + * TripleInOne* obj = new TripleInOne(stackSize); + * obj->push(stackNum,value); + * int param_2 = obj->pop(stackNum); + * int param_3 = obj->peek(stackNum); + * bool param_4 = obj->isEmpty(stackNum); + */ +``` + ```go type TripleInOne struct { - data []int - offset [3]int - stackSize int + cap int + stk []int } func Constructor(stackSize int) TripleInOne { - total := stackSize * 3 - data := make([]int, total) - offset := [3]int{} - for i := 0; i < 3; i++ { - offset[i] = i * stackSize - } - return TripleInOne{ - data: data, - offset: offset, - stackSize: stackSize, - } + return TripleInOne{stackSize, make([]int, stackSize*3+3)} } func (this *TripleInOne) Push(stackNum int, value int) { - i := this.offset[stackNum] - if i < (stackNum+1)*this.stackSize { - this.data[i] = value - this.offset[stackNum]++ + if this.stk[this.cap*3+stackNum] < this.cap { + this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] = value + this.stk[this.cap*3+stackNum]++ } } func (this *TripleInOne) Pop(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - this.offset[stackNum]-- - return this.data[i-1] + this.stk[this.cap*3+stackNum]-- + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] } func (this *TripleInOne) Peek(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - return this.data[i-1] + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]-1] } func (this *TripleInOne) IsEmpty(stackNum int) bool { - return this.offset[stackNum] == stackNum*this.stackSize + return this.stk[this.cap*3+stackNum] == 0 } /** @@ -182,6 +233,53 @@ func (this *TripleInOne) IsEmpty(stackNum int) bool { */ ``` +```ts +class TripleInOne { + private cap: number; + private stk: number[]; + + constructor(stackSize: number) { + this.cap = stackSize; + this.stk = Array(stackSize * 3 + 3).fill(0); + } + + push(stackNum: number, value: number): void { + if (this.stk[this.cap * 3 + stackNum] < this.cap) { + this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]] = value; + this.stk[this.cap * 3 + stackNum]++; + } + } + + pop(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + this.stk[this.cap * 3 + stackNum]--; + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]]; + } + + peek(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum] - 1]; + } + + isEmpty(stackNum: number): boolean { + return this.stk[this.cap * 3 + stackNum] === 0; + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * var obj = new TripleInOne(stackSize) + * obj.push(stackNum,value) + * var param_2 = obj.pop(stackNum) + * var param_3 = obj.peek(stackNum) + * var param_4 = obj.isEmpty(stackNum) + */ +``` + diff --git a/lcci/03.01.Three in One/Solution.cpp b/lcci/03.01.Three in One/Solution.cpp new file mode 100644 index 0000000000000..30868fbe07de4 --- /dev/null +++ b/lcci/03.01.Three in One/Solution.cpp @@ -0,0 +1,43 @@ +class TripleInOne { +public: + TripleInOne(int stackSize) { + cap = stackSize; + stk.resize(cap * 3 + 3); + } + + void push(int stackNum, int value) { + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; + } + } + + int pop(int stackNum) { + if (isEmpty(stackNum)) { + return -1; + } + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; + } + + int peek(int stackNum) { + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; + } + + bool isEmpty(int stackNum) { + return stk[cap * 3 + stackNum] == 0; + } + +private: + int cap; + vector stk; +}; + +/** + * Your TripleInOne object will be instantiated and called as such: + * TripleInOne* obj = new TripleInOne(stackSize); + * obj->push(stackNum,value); + * int param_2 = obj->pop(stackNum); + * int param_3 = obj->peek(stackNum); + * bool param_4 = obj->isEmpty(stackNum); + */ \ No newline at end of file diff --git a/lcci/03.01.Three in One/Solution.go b/lcci/03.01.Three in One/Solution.go index 8f2d4bf765e97..8152aea9f3eaf 100644 --- a/lcci/03.01.Three in One/Solution.go +++ b/lcci/03.01.Three in One/Solution.go @@ -1,50 +1,36 @@ type TripleInOne struct { - data []int - offset [3]int - stackSize int + cap int + stk []int } func Constructor(stackSize int) TripleInOne { - total := stackSize * 3 - data := make([]int, total) - offset := [3]int{} - for i := 0; i < 3; i++ { - offset[i] = i * stackSize - } - return TripleInOne{ - data: data, - offset: offset, - stackSize: stackSize, - } + return TripleInOne{stackSize, make([]int, stackSize*3+3)} } func (this *TripleInOne) Push(stackNum int, value int) { - i := this.offset[stackNum] - if i < (stackNum+1)*this.stackSize { - this.data[i] = value - this.offset[stackNum]++ + if this.stk[this.cap*3+stackNum] < this.cap { + this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] = value + this.stk[this.cap*3+stackNum]++ } } func (this *TripleInOne) Pop(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - this.offset[stackNum]-- - return this.data[i-1] + this.stk[this.cap*3+stackNum]-- + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]] } func (this *TripleInOne) Peek(stackNum int) int { - i := this.offset[stackNum] - if i == stackNum*this.stackSize { + if this.IsEmpty(stackNum) { return -1 } - return this.data[i-1] + return this.stk[this.cap*stackNum+this.stk[this.cap*3+stackNum]-1] } func (this *TripleInOne) IsEmpty(stackNum int) bool { - return this.offset[stackNum] == stackNum*this.stackSize + return this.stk[this.cap*3+stackNum] == 0 } /** diff --git a/lcci/03.01.Three in One/Solution.java b/lcci/03.01.Three in One/Solution.java index 19916ea47f1f3..a4d81dffd27f1 100644 --- a/lcci/03.01.Three in One/Solution.java +++ b/lcci/03.01.Three in One/Solution.java @@ -1,33 +1,33 @@ class TripleInOne { - private int[] s; - private int capacity; + private int cap; + private int[] stk; public TripleInOne(int stackSize) { - s = new int[stackSize * 3 + 3]; - capacity = stackSize; + cap = stackSize; + stk = new int[cap * 3 + 3]; } - + public void push(int stackNum, int value) { - if (s[stackNum + 3 * capacity] < capacity) { - s[s[stackNum + 3 * capacity] * 3 + stackNum] = value; - ++s[stackNum + 3 * capacity]; + if (stk[cap * 3 + stackNum] < cap) { + stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; + ++stk[cap * 3 + stackNum]; } } - + public int pop(int stackNum) { if (isEmpty(stackNum)) { return -1; } - --s[stackNum + 3 * capacity]; - return s[s[stackNum + 3 * capacity] * 3 + stackNum]; + --stk[cap * 3 + stackNum]; + return stk[cap * stackNum + stk[cap * 3 + stackNum]]; } - + public int peek(int stackNum) { - return isEmpty(stackNum) ? -1 : s[(s[stackNum + 3 * capacity] - 1) * 3 + stackNum]; + return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; } - + public boolean isEmpty(int stackNum) { - return s[stackNum + 3 * capacity] == 0; + return stk[cap * 3 + stackNum] == 0; } } diff --git a/lcci/03.01.Three in One/Solution.py b/lcci/03.01.Three in One/Solution.py index 9a381144f0421..155a822197c0b 100644 --- a/lcci/03.01.Three in One/Solution.py +++ b/lcci/03.01.Three in One/Solution.py @@ -1,20 +1,27 @@ class TripleInOne: + def __init__(self, stackSize: int): - self._capacity = stackSize - self._s = [[], [], []] + self.cap = stackSize + self.stk = [0] * (self.cap * 3 + 3) def push(self, stackNum: int, value: int) -> None: - if len(self._s[stackNum]) < self._capacity: - self._s[stackNum].append(value) + if self.stk[self.cap * 3 + stackNum] < self.cap: + self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] = value + self.stk[self.cap * 3 + stackNum] += 1 def pop(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum].pop() + if self.isEmpty(stackNum): + return -1 + self.stk[self.cap * 3 + stackNum] -= 1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum]] def peek(self, stackNum: int) -> int: - return -1 if self.isEmpty(stackNum) else self._s[stackNum][-1] + if self.isEmpty(stackNum): + return -1 + return self.stk[self.cap * stackNum + self.stk[self.cap * 3 + stackNum] - 1] def isEmpty(self, stackNum: int) -> bool: - return len(self._s[stackNum]) == 0 + return self.stk[self.cap * 3 + stackNum] == 0 # Your TripleInOne object will be instantiated and called as such: diff --git a/lcci/03.01.Three in One/Solution.ts b/lcci/03.01.Three in One/Solution.ts new file mode 100644 index 0000000000000..dafdde9ac8973 --- /dev/null +++ b/lcci/03.01.Three in One/Solution.ts @@ -0,0 +1,44 @@ +class TripleInOne { + private cap: number; + private stk: number[]; + + constructor(stackSize: number) { + this.cap = stackSize; + this.stk = Array(stackSize * 3 + 3).fill(0); + } + + push(stackNum: number, value: number): void { + if (this.stk[this.cap * 3 + stackNum] < this.cap) { + this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]] = value; + this.stk[this.cap * 3 + stackNum]++; + } + } + + pop(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + this.stk[this.cap * 3 + stackNum]--; + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum]]; + } + + peek(stackNum: number): number { + if (this.isEmpty(stackNum)) { + return -1; + } + return this.stk[this.cap * stackNum + this.stk[this.cap * 3 + stackNum] - 1]; + } + + isEmpty(stackNum: number): boolean { + return this.stk[this.cap * 3 + stackNum] === 0; + } +} + +/** + * Your TripleInOne object will be instantiated and called as such: + * var obj = new TripleInOne(stackSize) + * obj.push(stackNum,value) + * var param_2 = obj.pop(stackNum) + * var param_3 = obj.peek(stackNum) + * var param_4 = obj.isEmpty(stackNum) + */ From 771332e1227bd775f1bee2f57deeaec60ac604c7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 3 Apr 2024 10:07:35 +0800 Subject: [PATCH 2/2] fix: style --- lcci/03.01.Three in One/Solution.cpp | 8 ++++---- lcci/03.01.Three in One/Solution.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lcci/03.01.Three in One/Solution.cpp b/lcci/03.01.Three in One/Solution.cpp index 30868fbe07de4..1da6d47f122ac 100644 --- a/lcci/03.01.Three in One/Solution.cpp +++ b/lcci/03.01.Three in One/Solution.cpp @@ -4,14 +4,14 @@ class TripleInOne { cap = stackSize; stk.resize(cap * 3 + 3); } - + void push(int stackNum, int value) { if (stk[cap * 3 + stackNum] < cap) { stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; ++stk[cap * 3 + stackNum]; } } - + int pop(int stackNum) { if (isEmpty(stackNum)) { return -1; @@ -19,11 +19,11 @@ class TripleInOne { --stk[cap * 3 + stackNum]; return stk[cap * stackNum + stk[cap * 3 + stackNum]]; } - + int peek(int stackNum) { return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; } - + bool isEmpty(int stackNum) { return stk[cap * 3 + stackNum] == 0; } diff --git a/lcci/03.01.Three in One/Solution.java b/lcci/03.01.Three in One/Solution.java index a4d81dffd27f1..b1c163cb17683 100644 --- a/lcci/03.01.Three in One/Solution.java +++ b/lcci/03.01.Three in One/Solution.java @@ -6,14 +6,14 @@ public TripleInOne(int stackSize) { cap = stackSize; stk = new int[cap * 3 + 3]; } - + public void push(int stackNum, int value) { if (stk[cap * 3 + stackNum] < cap) { stk[cap * stackNum + stk[cap * 3 + stackNum]] = value; ++stk[cap * 3 + stackNum]; } } - + public int pop(int stackNum) { if (isEmpty(stackNum)) { return -1; @@ -21,11 +21,11 @@ public int pop(int stackNum) { --stk[cap * 3 + stackNum]; return stk[cap * stackNum + stk[cap * 3 + stackNum]]; } - + public int peek(int stackNum) { return isEmpty(stackNum) ? -1 : stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]; } - + public boolean isEmpty(int stackNum) { return stk[cap * 3 + stackNum] == 0; }