File tree Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Expand file tree Collapse file tree 4 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ Solution .java
Original file line number Diff line number Diff line change
1
+ class MinStack {
2
+
3
+ static class Stack {
4
+
5
+ static class Node {
6
+ int val ;
7
+ Node next ;
8
+ Node prev ;
9
+ }
10
+
11
+ Node head = new Node ();
12
+ int size = 0 ;
13
+
14
+ public void push (int x ) {
15
+ Node n = new Node ();
16
+ n .val = x ;
17
+ n .prev = head ;
18
+
19
+ head .next = n ;
20
+ head = n ;
21
+
22
+ size ++;
23
+ }
24
+
25
+ public void pop () {
26
+ head = head .prev ;
27
+ size --;
28
+ }
29
+
30
+ public int top () {
31
+ return head .val ;
32
+ }
33
+ }
34
+
35
+ Stack data = new Stack ();
36
+ Stack mins = new Stack ();
37
+
38
+ public void push (int x ) {
39
+ data .push (x );
40
+
41
+ if (mins .size == 0 || getMin () >= x ){
42
+ mins .push (x );
43
+ }
44
+ }
45
+
46
+ public void pop () {
47
+ int last = data .top ();
48
+ data .pop ();
49
+
50
+ if (last <= getMin ()){
51
+ mins .pop ();
52
+ }
53
+ }
54
+
55
+ public int top () {
56
+ return data .top ();
57
+ }
58
+
59
+ public int getMin () {
60
+ if (mins .size > 0 ){
61
+ return mins .top ();
62
+ }
63
+
64
+ throw new IllegalStateException ();
65
+ }
66
+ }
Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : solution
3
+ title : Min Stack
4
+ date : 2014-11-12 12:19:55+08:00
5
+ ---
6
+ {% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
+ {% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
8
+ {% include {{leetcode_readme}} %}
You can’t perform that action at this time.
0 commit comments