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

Commit714d364

Browse files
refactor 155
1 parent90d0a21 commit714d364

File tree

1 file changed

+35
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+35
-35
lines changed
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
packagecom.fishercoder.solutions;
22

3-
43
importjava.util.Stack;
54

65
/**
@@ -25,47 +24,48 @@
2524

2625
publicclass_155 {
2726

28-
publicstaticclassMinStack {
29-
privateStack<Integer>stack;
30-
privateintmin;
31-
32-
/**
33-
* initialize your data structure here.
34-
*/
35-
publicMinStack() {
36-
stack =newStack();
37-
min =Integer.MAX_VALUE;
38-
}
27+
publicstaticclassSolution1 {
28+
classMinStack {
29+
privateStack<Integer>stack;
30+
privateintmin;
3931

40-
publicvoidpush(intx) {
41-
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
42-
* this way, when popping, we could always get the next minimum one in constant time.*/
43-
if (x <=min) {
44-
stack.push(min);
45-
min =x;
32+
/**
33+
* initialize your data structure here.
34+
*/
35+
publicMinStack() {
36+
stack =newStack();
37+
min =Integer.MAX_VALUE;
4638
}
47-
stack.push(x);
48-
}
4939

50-
publicvoidpop() {
51-
if (min ==stack.peek()) {
52-
stack.pop();
53-
min =stack.pop();
54-
}else {
55-
stack.pop();
40+
publicvoidpush(intx) {
41+
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
42+
* this way, when popping, we could always get the next minimum one in constant time.*/
43+
if (x <=min) {
44+
stack.push(min);
45+
min =x;
46+
}
47+
stack.push(x);
5648
}
57-
if (stack.isEmpty()) {
58-
min =Integer.MAX_VALUE;
49+
50+
publicvoidpop() {
51+
if (min ==stack.peek()) {
52+
stack.pop();
53+
min =stack.pop();
54+
}else {
55+
stack.pop();
56+
}
57+
if (stack.isEmpty()) {
58+
min =Integer.MAX_VALUE;
59+
}
5960
}
60-
}
6161

62-
publicinttop() {
63-
returnstack.peek();
64-
}
62+
publicinttop() {
63+
returnstack.peek();
64+
}
6565

66-
publicintgetMin() {
67-
returnmin;
66+
publicintgetMin() {
67+
returnmin;
68+
}
6869
}
6970
}
70-
7171
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp