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

Commit65745af

Browse files
refactor 402
1 parent26c7e48 commit65745af

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@
2525
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
2626
*/
2727
publicclass_402 {
28-
29-
/**credit: https://discuss.leetcode.com/topic/59412/a-greedy-method-using-stack-o-n-time-and-o-n-space*/
30-
publicStringremoveKdigits(Stringnum,intk) {
31-
intdigits =num.length() -k;
32-
char[]stack =newchar[num.length()];
33-
inttop =0;
34-
35-
for (inti =0;i <num.length();i++) {
36-
charc =num.charAt(i);
37-
while (top >0 &&stack[top -1] >c &&k >0) {
38-
top--;
39-
k--;
28+
publicstaticclassSolution1 {
29+
30+
/** credit: https://discuss.leetcode.com/topic/59412/a-greedy-method-using-stack-o-n-time-and-o-n-space */
31+
publicStringremoveKdigits(Stringnum,intk) {
32+
intdigits =num.length() -k;
33+
char[]stack =newchar[num.length()];
34+
inttop =0;
35+
36+
for (inti =0;i <num.length();i++) {
37+
charc =num.charAt(i);
38+
while (top >0 &&stack[top -1] >c &&k >0) {
39+
top--;
40+
k--;
41+
}
42+
stack[top++] =c;
4043
}
41-
stack[top++] =c;
42-
}
4344

44-
intindex =0;
45-
while (index <digits &&stack[index] =='0') {
46-
index++;
45+
intindex =0;
46+
while (index <digits &&stack[index] =='0') {
47+
index++;
48+
}
49+
returnindex ==digits ?"0" :newString(stack,index,digits -index);
4750
}
48-
returnindex ==digits ?"0" :newString(stack,index,digits -index);
4951
}
50-
5152
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66

77
importstaticorg.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/3/17.
11-
*/
129
publicclass_402Test {
13-
privatestatic_402test;
10+
privatestatic_402.Solution1solution1;
1411

15-
@BeforeClass
16-
publicstaticvoidsetup() {
17-
test=new_402();
18-
}
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1=new_402.Solution1();
15+
}
1916

20-
@Test
21-
publicvoidtest1() {
22-
assertEquals("1219",test.removeKdigits("1432219",3));
23-
}
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("1219",solution1.removeKdigits("1432219",3));
20+
}
2421
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp