Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46ea5b9

Browse files
authored
added array solution for 1381 with test cases (fishercoder1534#127)
1 parentc25b0fb commit46ea5b9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_1381.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,43 @@ public void increment(int k, int val) {
7979
}
8080
}
8181
}
82+
83+
/**
84+
* Implementation of Stack using Array
85+
*/
86+
publicstaticclassSolution2{
87+
publicstaticclassCustomStack {
88+
privateinttop;
89+
privateintmaxSize;
90+
privateintstack[];
91+
publicCustomStack(intmaxSize) {
92+
this.maxSize =maxSize;
93+
this.stack =newint[maxSize];
94+
}
95+
96+
publicvoidpush(intx) {
97+
if(top ==maxSize)return;
98+
stack[top] =x;
99+
top++;
100+
}
101+
102+
publicintpop() {
103+
if(top ==0)
104+
return -1;
105+
intpopValue =stack[top-1];
106+
stack[top-1] =0;
107+
top--;
108+
returnpopValue;
109+
}
110+
111+
publicvoidincrement(intk,intval) {
112+
if(top ==0 ||k ==0)return;
113+
for(inti =0;i<k;i++){
114+
if(i ==top)
115+
break;
116+
stack[i] +=val;
117+
}
118+
}
119+
}
120+
}
82121
}

‎src/test/java/com/fishercoder/_1381Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
publicclass_1381Test {
99
privatestatic_1381.Solution1.CustomStackcustomStack;
10+
privatestatic_1381.Solution2.CustomStackcustomStack2;
1011

1112
@Test
1213
publicvoidtest1() {
@@ -24,5 +25,20 @@ public void test1() {
2425
assertEquals(201,customStack.pop());
2526
assertEquals(-1,customStack.pop());
2627
}
28+
@Test
29+
publicvoidtest2() {
30+
customStack2 =new_1381.Solution2.CustomStack(3);
31+
customStack2.push(-1);
32+
customStack2.push(20);
33+
assertEquals(20,customStack2.pop());
34+
customStack2.push(30);
35+
customStack2.push(40);
36+
customStack2.push(50);
37+
customStack2.increment(5,100);
38+
assertEquals(140,customStack2.pop());
39+
assertEquals(130,customStack2.pop());
40+
assertEquals(99,customStack2.pop());
41+
42+
}
2743

2844
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp