Skip to content

Commit 8e4e469

Browse files
authored
feat: add java solution to lcci problem: No.03.03 (doocs#865)
No.03.03. Stack of Plates LCCI
1 parent 2c93c60 commit 8e4e469

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lcci/03.03.Stack of Plates/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,40 @@
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```java
44+
class StackOfPlates {
45+
List<Stack<Integer>> list = new ArrayList<>();
46+
int cap = 0;
47+
48+
public StackOfPlates(int cap) {
49+
this.cap = cap;
50+
}
51+
52+
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);
56+
}
57+
58+
public int pop() {
59+
return popAt(list.size() - 1);
60+
}
4461

62+
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;
68+
}
69+
}
70+
71+
/**
72+
* Your StackOfPlates object will be instantiated and called as such:
73+
* StackOfPlates obj = new StackOfPlates(cap);
74+
* obj.push(val);
75+
* int param_2 = obj.pop();
76+
* int param_3 = obj.popAt(index);
77+
*/
4578
```
4679

4780
### **TypeScript**

0 commit comments

Comments
 (0)