File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
lcci/03.03.Stack of Plates Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 41
41
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
42
43
43
``` 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
+ }
44
61
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
+ */
45
78
```
46
79
47
80
### ** TypeScript**
You can’t perform that action at this time.
0 commit comments