File tree Expand file tree Collapse file tree 3 files changed +169
-0
lines changed Expand file tree Collapse file tree 3 files changed +169
-0
lines changed Original file line number Diff line number Diff line change
1
+ # [ 面试题30. 包含min函数的栈] ( https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/ )
2
+
3
+ ## 题目描述
4
+ 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
5
+
6
+ ** 示例:**
7
+
8
+ ```
9
+ MinStack minStack = new MinStack();
10
+ minStack.push(-2);
11
+ minStack.push(0);
12
+ minStack.push(-3);
13
+ minStack.min(); --> 返回 -3.
14
+ minStack.pop();
15
+ minStack.top(); --> 返回 0.
16
+ minStack.min(); --> 返回 -2.
17
+ ```
18
+
19
+ ** 提示:**
20
+
21
+ - 各函数的调用总次数不超过 20000 次
22
+
23
+ ## 解法
24
+ ### Python3
25
+ ``` python
26
+ class MinStack :
27
+
28
+ def __init__ (self ):
29
+ """
30
+ initialize your data structure here.
31
+ """
32
+ self ._s1 = []
33
+ self ._s2 = []
34
+
35
+ def push (self , x : int ) -> None :
36
+ self ._s1.append(x)
37
+ self ._s2.append(x if len (self ._s2) == 0 or self ._s2[- 1 ] > x else self ._s2[- 1 ])
38
+
39
+ def pop (self ) -> None :
40
+ self ._s1.pop()
41
+ self ._s2.pop()
42
+
43
+ def top (self ) -> int :
44
+ return self ._s1[- 1 ]
45
+
46
+ def min (self ) -> int :
47
+ return self ._s2[- 1 ]
48
+
49
+
50
+ # Your MinStack object will be instantiated and called as such:
51
+ # obj = MinStack()
52
+ # obj.push(x)
53
+ # obj.pop()
54
+ # param_3 = obj.top()
55
+ # param_4 = obj.min()
56
+ ```
57
+
58
+ ### Java
59
+ ``` java
60
+ class MinStack {
61
+ private Stack<Integer > s1;
62
+ private Stack<Integer > s2;
63
+
64
+ /* * initialize your data structure here. */
65
+ public MinStack () {
66
+ s1 = new Stack<> ();
67
+ s2 = new Stack<> ();
68
+ }
69
+
70
+ public void push (int x ) {
71
+ s1. push(x);
72
+ s2. push((s2. empty() || s2. peek() > x) ? x : s2. peek());
73
+ }
74
+
75
+ public void pop () {
76
+ s1. pop();
77
+ s2. pop();
78
+ }
79
+
80
+ public int top () {
81
+ return s1. peek();
82
+ }
83
+
84
+ public int min () {
85
+ return s2. peek();
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Your MinStack object will be instantiated and called as such:
91
+ * MinStack obj = new MinStack();
92
+ * obj.push(x);
93
+ * obj.pop();
94
+ * int param_3 = obj.top();
95
+ * int param_4 = obj.min();
96
+ */
97
+ ```
98
+
99
+ ### ...
100
+ ```
101
+
102
+ ```
Original file line number Diff line number Diff line change
1
+ class MinStack {
2
+ private Stack <Integer > s1 ;
3
+ private Stack <Integer > s2 ;
4
+
5
+ /** initialize your data structure here. */
6
+ public MinStack () {
7
+ s1 = new Stack <>();
8
+ s2 = new Stack <>();
9
+ }
10
+
11
+ public void push (int x ) {
12
+ s1 .push (x );
13
+ s2 .push ((s2 .empty () || s2 .peek () > x ) ? x : s2 .peek ());
14
+ }
15
+
16
+ public void pop () {
17
+ s1 .pop ();
18
+ s2 .pop ();
19
+ }
20
+
21
+ public int top () {
22
+ return s1 .peek ();
23
+ }
24
+
25
+ public int min () {
26
+ return s2 .peek ();
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Your MinStack object will be instantiated and called as such:
32
+ * MinStack obj = new MinStack();
33
+ * obj.push(x);
34
+ * obj.pop();
35
+ * int param_3 = obj.top();
36
+ * int param_4 = obj.min();
37
+ */
Original file line number Diff line number Diff line change
1
+ class MinStack :
2
+
3
+ def __init__ (self ):
4
+ """
5
+ initialize your data structure here.
6
+ """
7
+ self ._s1 = []
8
+ self ._s2 = []
9
+
10
+ def push (self , x : int ) -> None :
11
+ self ._s1 .append (x )
12
+ self ._s2 .append (x if len (self ._s2 ) == 0 or self ._s2 [- 1 ] > x else self ._s2 [- 1 ])
13
+
14
+ def pop (self ) -> None :
15
+ self ._s1 .pop ()
16
+ self ._s2 .pop ()
17
+
18
+ def top (self ) -> int :
19
+ return self ._s1 [- 1 ]
20
+
21
+ def min (self ) -> int :
22
+ return self ._s2 [- 1 ]
23
+
24
+
25
+ # Your MinStack object will be instantiated and called as such:
26
+ # obj = MinStack()
27
+ # obj.push(x)
28
+ # obj.pop()
29
+ # param_3 = obj.top()
30
+ # param_4 = obj.min()
You can’t perform that action at this time.
0 commit comments