FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/MinStack.java at master · tinycedar/leetcode · GitHub
tinycedar
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
MinStack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (74 loc) · 1.51 KB
Breadcrumbs
leetcode
/
MinStack.java
Copy path
File metadata and controls
87 lines (74 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
List
;
/**
*
*/
/**
* @author qienl
*
*/
public
class
MinStack
{
private
int
size
;
private
Node
head
=
null
;
private
Integer
min
=
null
;
private
class
Node
{
int
value
;
Node
next
;
public
Node
(
int
value
,
Node
next
) {
this
.
value
=
value
;
this
.
next
=
next
;
}
}
public
MinStack
() {
}
public
void
push
(
int
x
) {
this
.
size
++;
this
.
head
=
new
Node
(
x
,
this
.
head
);
if
(
this
.
min
==
null
||
x
<
this
.
min
) {
this
.
min
=
x
;
}
}
public
void
pop
() {
if
(
head
!=
null
) {
size
--;
int
top
=
head
.
value
;
head
=
head
.
next
;
if
(
head
==
null
) {
this
.
min
=
null
;
}
else
if
(
min
!=
null
&&
top
==
min
) {
List
<
Integer
>
tempList
=
new
ArrayList
<>(
this
.
size
);
for
(
Node
temp
=
head
;
temp
!=
null
;
temp
=
temp
.
next
) {
if
(
min
==
temp
.
value
) {
return
;
}
tempList
.
add
(
temp
.
value
);
}
Collections
.
sort
(
tempList
);
this
.
min
=
tempList
.
get
(
0
);
}
}
}
public
int
top
() {
if
(
head
!=
null
) {
return
head
.
value
;
}
throw
new
RuntimeException
(
"Empty stack"
);
}
public
int
getMin
() {
if
(
head
!=
null
&&
min
!=
null
) {
return
this
.
min
;
}
throw
new
RuntimeException
(
"Empty stack"
);
}
public
static
void
main
(
String
[]
args
) {
MinStack
obj
=
new
MinStack
();
obj
.
push
(-
2
);
obj
.
push
(
0
);
obj
.
push
(-
3
);
obj
.
getMin
();
obj
.
pop
();
obj
.
top
();
System
.
out
.
println
(
obj
.
getMin
());
}
}
Back
|
FazBrowse Home
|
New Git URL