Skip to content

Commit 393242a

Browse files
committed
feat: add solutions to lcci problem: No.03.03
No.03.03.Stack of Plates
1 parent 8e4e469 commit 393242a

File tree

9 files changed

+468
-19
lines changed

9 files changed

+468
-19
lines changed

lcci/03.03.Stack of Plates/README.md

+144-11
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,47 @@
2626

2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
**方法一:模拟**
30+
31+
用列表模拟栈的集合,每个栈的容量为 `cap`,当栈满时,新建一个栈。
32+
2933
<!-- tabs:start -->
3034

3135
### **Python3**
3236

3337
<!-- 这里可写当前语言的特殊实现逻辑 -->
3438

3539
```python
40+
class StackOfPlates:
41+
42+
def __init__(self, cap: int):
43+
self.cap = cap
44+
self.stk = []
45+
46+
def push(self, val: int) -> None:
47+
if self.cap == 0:
48+
return
49+
if not self.stk or len(self.stk[-1]) >= self.cap:
50+
self.stk.append([])
51+
self.stk[-1].append(val)
52+
53+
def pop(self) -> int:
54+
return self.popAt(len(self.stk) - 1)
55+
56+
def popAt(self, index: int) -> int:
57+
ans = -1
58+
if 0 <= index < len(self.stk):
59+
ans = self.stk[index].pop()
60+
if not self.stk[index]:
61+
self.stk.pop(index)
62+
return ans
3663

64+
65+
# Your StackOfPlates object will be instantiated and called as such:
66+
# obj = StackOfPlates(cap)
67+
# obj.push(val)
68+
# param_2 = obj.pop()
69+
# param_3 = obj.popAt(index)
3770
```
3871

3972
### **Java**
@@ -42,29 +75,36 @@
4275

4376
```java
4477
class StackOfPlates {
45-
List<Stack<Integer>> list = new ArrayList<>();
46-
int cap = 0;
78+
private List<Deque<Integer>> stk = new ArrayList<>();
79+
private int cap;
4780

4881
public StackOfPlates(int cap) {
4982
this.cap = cap;
5083
}
5184

5285
public void push(int val) {
53-
if (this.cap == 0) return;
54-
if (list.isEmpty() || list.get(list.size() - 1).size() == cap) list.add(new Stack<>());
55-
list.get(list.size() - 1).push(val);
86+
if (cap == 0) {
87+
return;
88+
}
89+
if (stk.isEmpty() || stk.get(stk.size() - 1).size() >= cap) {
90+
stk.add(new ArrayDeque<>());
91+
}
92+
stk.get(stk.size() - 1).push(val);
5693
}
5794

5895
public int pop() {
59-
return popAt(list.size() - 1);
96+
return popAt(stk.size() - 1);
6097
}
6198

6299
public int popAt(int index) {
63-
if (index >= list.size() || index < 0) return -1;
64-
Stack<Integer> s = list.get(index);
65-
int res = s.pop();
66-
if (s.isEmpty()) list.remove(index);
67-
return res;
100+
int ans = -1;
101+
if (index >= 0 && index < stk.size()) {
102+
ans = stk.get(index).pop();
103+
if (stk.get(index).isEmpty()) {
104+
stk.remove(index);
105+
}
106+
}
107+
return ans;
68108
}
69109
}
70110

@@ -77,6 +117,99 @@ class StackOfPlates {
77117
*/
78118
```
79119

120+
### **C++**
121+
122+
```cpp
123+
class StackOfPlates {
124+
public:
125+
StackOfPlates(int cap) {
126+
this->cap = cap;
127+
}
128+
129+
void push(int val) {
130+
if (!cap) return;
131+
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
132+
stk[stk.size() - 1].push(val);
133+
}
134+
135+
int pop() {
136+
return popAt(stk.size() - 1);
137+
}
138+
139+
int popAt(int index) {
140+
int ans = -1;
141+
if (index >= 0 && index < stk.size()) {
142+
ans = stk[index].top();
143+
stk[index].pop();
144+
if (stk[index].empty()) {
145+
stk.erase(stk.begin() + index);
146+
}
147+
}
148+
return ans;
149+
}
150+
151+
private:
152+
vector<stack<int>> stk;
153+
int cap;
154+
};
155+
156+
/**
157+
* Your StackOfPlates object will be instantiated and called as such:
158+
* StackOfPlates* obj = new StackOfPlates(cap);
159+
* obj->push(val);
160+
* int param_2 = obj->pop();
161+
* int param_3 = obj->popAt(index);
162+
*/
163+
```
164+
165+
### **Go**
166+
167+
```go
168+
type StackOfPlates struct {
169+
stk [][]int
170+
cap int
171+
}
172+
173+
func Constructor(cap int) StackOfPlates {
174+
return StackOfPlates{[][]int{}, cap}
175+
}
176+
177+
func (this *StackOfPlates) Push(val int) {
178+
if this.cap == 0 {
179+
return
180+
}
181+
if len(this.stk) == 0 || len(this.stk[len(this.stk)-1]) >= this.cap {
182+
this.stk = append(this.stk, []int{})
183+
}
184+
this.stk[len(this.stk)-1] = append(this.stk[len(this.stk)-1], val)
185+
}
186+
187+
func (this *StackOfPlates) Pop() int {
188+
return this.PopAt(len(this.stk) - 1)
189+
}
190+
191+
func (this *StackOfPlates) PopAt(index int) int {
192+
ans := -1
193+
if index >= 0 && index < len(this.stk) {
194+
t := this.stk[index]
195+
ans = t[len(t)-1]
196+
this.stk[index] = this.stk[index][:len(t)-1]
197+
if len(this.stk[index]) == 0 {
198+
this.stk = append(this.stk[:index], this.stk[index+1:]...)
199+
}
200+
}
201+
return ans
202+
}
203+
204+
/**
205+
* Your StackOfPlates object will be instantiated and called as such:
206+
* obj := Constructor(cap);
207+
* obj.Push(val);
208+
* param_2 := obj.Pop();
209+
* param_3 := obj.PopAt(index);
210+
*/
211+
```
212+
80213
### **TypeScript**
81214

82215
```ts

lcci/03.03.Stack of Plates/README_EN.md

+162
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,175 @@
4444
### **Python3**
4545

4646
```python
47+
class StackOfPlates:
4748

49+
def __init__(self, cap: int):
50+
self.cap = cap
51+
self.stk = []
52+
53+
def push(self, val: int) -> None:
54+
if self.cap == 0:
55+
return
56+
if not self.stk or len(self.stk[-1]) >= self.cap:
57+
self.stk.append([])
58+
self.stk[-1].append(val)
59+
60+
def pop(self) -> int:
61+
return self.popAt(len(self.stk) - 1)
62+
63+
def popAt(self, index: int) -> int:
64+
ans = -1
65+
if 0 <= index < len(self.stk):
66+
ans = self.stk[index].pop()
67+
if not self.stk[index]:
68+
self.stk.pop(index)
69+
return ans
70+
71+
72+
# Your StackOfPlates object will be instantiated and called as such:
73+
# obj = StackOfPlates(cap)
74+
# obj.push(val)
75+
# param_2 = obj.pop()
76+
# param_3 = obj.popAt(index)
4877
```
4978

5079
### **Java**
5180

5281
```java
82+
class StackOfPlates {
83+
private List<Deque<Integer>> stk = new ArrayList<>();
84+
private int cap;
5385

86+
public StackOfPlates(int cap) {
87+
this.cap = cap;
88+
}
89+
90+
public void push(int val) {
91+
if (cap == 0) {
92+
return;
93+
}
94+
if (stk.isEmpty() || stk.get(stk.size() - 1).size() >= cap) {
95+
stk.add(new ArrayDeque<>());
96+
}
97+
stk.get(stk.size() - 1).push(val);
98+
}
99+
100+
public int pop() {
101+
return popAt(stk.size() - 1);
102+
}
103+
104+
public int popAt(int index) {
105+
int ans = -1;
106+
if (index >= 0 && index < stk.size()) {
107+
ans = stk.get(index).pop();
108+
if (stk.get(index).isEmpty()) {
109+
stk.remove(index);
110+
}
111+
}
112+
return ans;
113+
}
114+
}
115+
116+
/**
117+
* Your StackOfPlates object will be instantiated and called as such:
118+
* StackOfPlates obj = new StackOfPlates(cap);
119+
* obj.push(val);
120+
* int param_2 = obj.pop();
121+
* int param_3 = obj.popAt(index);
122+
*/
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class StackOfPlates {
129+
public:
130+
StackOfPlates(int cap) {
131+
this->cap = cap;
132+
}
133+
134+
void push(int val) {
135+
if (!cap) return;
136+
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
137+
stk[stk.size() - 1].push(val);
138+
}
139+
140+
int pop() {
141+
return popAt(stk.size() - 1);
142+
}
143+
144+
int popAt(int index) {
145+
int ans = -1;
146+
if (index >= 0 && index < stk.size()) {
147+
ans = stk[index].top();
148+
stk[index].pop();
149+
if (stk[index].empty()) {
150+
stk.erase(stk.begin() + index);
151+
}
152+
}
153+
return ans;
154+
}
155+
156+
private:
157+
vector<stack<int>> stk;
158+
int cap;
159+
};
160+
161+
/**
162+
* Your StackOfPlates object will be instantiated and called as such:
163+
* StackOfPlates* obj = new StackOfPlates(cap);
164+
* obj->push(val);
165+
* int param_2 = obj->pop();
166+
* int param_3 = obj->popAt(index);
167+
*/
168+
```
169+
170+
### **Go**
171+
172+
```go
173+
type StackOfPlates struct {
174+
stk [][]int
175+
cap int
176+
}
177+
178+
func Constructor(cap int) StackOfPlates {
179+
return StackOfPlates{[][]int{}, cap}
180+
}
181+
182+
func (this *StackOfPlates) Push(val int) {
183+
if this.cap == 0 {
184+
return
185+
}
186+
if len(this.stk) == 0 || len(this.stk[len(this.stk)-1]) >= this.cap {
187+
this.stk = append(this.stk, []int{})
188+
}
189+
this.stk[len(this.stk)-1] = append(this.stk[len(this.stk)-1], val)
190+
}
191+
192+
func (this *StackOfPlates) Pop() int {
193+
return this.PopAt(len(this.stk) - 1)
194+
}
195+
196+
func (this *StackOfPlates) PopAt(index int) int {
197+
ans := -1
198+
if index >= 0 && index < len(this.stk) {
199+
t := this.stk[index]
200+
ans = t[len(t)-1]
201+
this.stk[index] = this.stk[index][:len(t)-1]
202+
if len(this.stk[index]) == 0 {
203+
this.stk = append(this.stk[:index], this.stk[index+1:]...)
204+
}
205+
}
206+
return ans
207+
}
208+
209+
/**
210+
* Your StackOfPlates object will be instantiated and called as such:
211+
* obj := Constructor(cap);
212+
* obj.Push(val);
213+
* param_2 := obj.Pop();
214+
* param_3 := obj.PopAt(index);
215+
*/
54216
```
55217

56218
### **TypeScript**

0 commit comments

Comments
 (0)