There was an error while loading. Please reload this page.
1 parent 2dbc65b commit 4125b55Copy full SHA for 4125b55
Java/Design-a-Stack-With-Increment-Operation.java
@@ -0,0 +1,35 @@
1
+class CustomStack {
2
+ int[] stack;
3
+ public int top=-1;
4
+ public CustomStack(int maxSize) {
5
+ stack=new int[maxSize];
6
+ }
7
+
8
+ public void push(int x) {
9
+ if(top<stack.length-1)
10
+ {
11
+ stack[++top]=x;
12
13
14
15
+ public int pop() {
16
+ if(top==-1)
17
+ return -1;
18
+ top--;
19
+ return stack[top+1];
20
21
22
+ public void increment(int k, int val) {
23
+ for(int i=0;i<(k>top+1?top+1:k);i++) {
24
+ stack[i]+=val;
25
26
27
+}
28
29
+/**
30
+ * Your CustomStack object will be instantiated and called as such:
31
+ * CustomStack obj = new CustomStack(maxSize);
32
+ * obj.push(x);
33
+ * int param_2 = obj.pop();
34
+ * obj.increment(k,val);
35
+ */
0 commit comments