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

Appearance settings

Latest commit

 

History

History
History
93 lines (77 loc) · 1.88 KB

File metadata and controls

93 lines (77 loc) · 1.88 KB
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
88
89
90
91
92
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant
* time.
*
* push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() --
* Get the top element. getMin() -- Retrieve the minimum element in the stack.
*
* Example:
* MinStack minStack = new MinStack();
* minStack.push(-2);
* minStack.push(0);
* minStack.push(-3);
* minStack.getMin(); --> Returns -3.
* minStack.pop();
* minStack.top(); --> Returns 0.
* minStack.getMin(); --> Returns -2.
*
* @authorLiu.3502
* @created2018-01-31 下午6:45
*/
public class LC155_MinStack{
public static void main(String[] args) {
}
//Wapper
class MinStackNode {
public Node ans;
public void push(int x) {
ans = ans == null ? new Node(x, x) : new Node(x, Math.min(x, ans.min), ans);
}
public void pop() {
ans = ans.next;
}
public int top() {
return ans.val;
}
public int getMin() {
return ans.min;
}
private class Node {
int val;
int min;
Node next;
private Node(int val, int min) {
this(val, min, null);
}
private Node(int val, int min, Node next) {
this.val = val;
this.min = min;
this.next = next;
}
}
}
//linklist
class MinStackList {
ArrayList<Integer> list;
public MinStackList() {
list = new ArrayList<Integer>();
}
public void push(int x) {
list.add(x);
}
public void pop() {
if (list.size() != 0) {
list.remove(list.size() - 1);
}
}
public int top() {
return list.get(list.size() - 1);
}
//Get Min element from stack
public int getMin() {
return list.stream().mapToInt(v -> v).min().orElseThrow(NoSuchElementException::new);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.