forked from kexun/jianzhioffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.java
137 lines (104 loc) · 2.16 KB
/
Day11.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.offer;
public class Day11 {
public MyStack stack;
public static void main(String[] args) {
Day11 d = new Day11();
d.stack.push(3);
d.stack.push(4);
d.stack.push(37);
d.stack.push(10);
d.stack.push(13);
d.stack.push(32);
d.stack.push(67);
d.stack.push(2);
d.stack.push(9);
d.stack.push(45);
System.out.println(d.stack.getMin().data);
}
public Day11() {
this.stack = new MyStack();
}
/**
* 自定义栈, 能在O(1) 时间内执行 push pop 获取最小值的操作
* @author kexun
*
*/
class MyStack {
private Node head;
private Node sortedHead;
public MyStack() {
this.head = new Node(0);
this.sortedHead = new Node(0);
}
/**
* 正常入栈 但是要维护一个排序的栈
* @param data
*/
public void push(int data) {
Node node = new Node(data);
node.next = head.next;
this.head.next = node;
pushSortedList(data);
}
/**
* 正常出栈
* @return
*/
public Node pop() {
if (head.next == null) {
return null;
}
Node value = head.next;
head.next = value.next;
popSortedList(value);
return value;
}
/**
* 入栈 并排序
* @param data
*/
public void pushSortedList(int data) {
Node first = sortedHead.next;
if (first == null) {
Node node = new Node(data);
sortedHead.next = node;
return;
}
if (data < first.data) {
Node node = new Node(data);
node.next = first;
sortedHead.next = node;
} else {
Node node = new Node(first.data);
node.next = first;
sortedHead.next = node;
}
}
/**
* 如果当前出栈的节点等于当前最小节点 则出栈
* @param node
*/
public void popSortedList(Node node) {
if (node == null || sortedHead.next == null)
return;
Node first = sortedHead.next;
if (node.data == first.data) {
sortedHead.next = first.next;
}
}
/**
* 获取当前栈的最小节点
* @return
*/
public Node getMin() {
return sortedHead.next;
}
}
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
}