1
- /* *
1
+ /*
2
2
* @Author: Chacha
3
3
* @Date: 2018-12-05 22:58:19
4
4
* @Last Modified by: Chacha
5
- * @Last Modified time: 2018-12-08 22:23:42
5
+ * @Last Modified time: 2021-03-07 22:08:39
6
6
*/
7
7
8
8
#include < iostream>
9
9
#include < vector>
10
10
#include < stack>
11
11
using namespace std ;
12
12
13
- class MyStack {
14
- private:
15
- vector<int > data; // store elements
16
-
17
- public:
18
- /* * Insert an element into the stack. */
19
- void push (int x) {
20
- data.push_back (x);
21
- }
13
+ class MyStack
14
+ {
15
+ private:
16
+ vector<int > data; // store elements
22
17
23
- /* * Checks whether the queue is empty or not. */
24
- bool isEmpty () {
25
- return data.empty ();
26
- }
18
+ public:
19
+ /* * Insert an element into the stack. */
20
+ void push (int x)
21
+ {
22
+ data.push_back (x);
23
+ }
27
24
28
- /* * Get the top item from the queue. */
29
- int top () {
30
- return data.back ();
31
- }
25
+ /* * Checks whether the queue is empty or not. */
26
+ bool isEmpty ()
27
+ {
28
+ return data.empty ();
29
+ }
32
30
33
- /* * Delete an element from the queue. Return true if the operation is successful . */
34
- bool pop () {
35
- if ( isEmpty ()) {
36
- return false ;
37
- }
31
+ /* * Get the top item from the queue . */
32
+ int top ()
33
+ {
34
+ return data. back () ;
35
+ }
38
36
39
- data.pop_back ();
40
- return true ;
37
+ /* * Delete an element from the queue. Return true if the operation is successful. */
38
+ bool pop ()
39
+ {
40
+ if (isEmpty ())
41
+ {
42
+ return false ;
41
43
}
44
+
45
+ data.pop_back ();
46
+ return true ;
47
+ }
42
48
};
43
49
44
50
/* **********************************************************************************
@@ -60,30 +66,37 @@ class MyStack {
60
66
*
61
67
* Source: https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/877/
62
68
************************************************************************************/
63
- class MinStack {
64
- public:
65
- stack<int > s1;
66
- stack<int > s2;
67
-
68
- MinStack () {
69
-
69
+ class MinStack
70
+ {
71
+ public:
72
+ stack<int > s1;
73
+ stack<int > s2;
74
+
75
+ MinStack ()
76
+ {
70
77
}
71
78
72
- void push (int x) {
79
+ void push (int x)
80
+ {
73
81
s1.push (x);
74
- if (s2.empty () || x <= getMin ()) s2.push (x);
82
+ if (s2.empty () || x <= getMin ())
83
+ s2.push (x);
75
84
}
76
85
77
- void pop () {
78
- if (s1.top () == getMin ()) s2.pop ();
86
+ void pop ()
87
+ {
88
+ if (s1.top () == getMin ())
89
+ s2.pop ();
79
90
s1.pop ();
80
91
}
81
92
82
- int top () {
93
+ int top ()
94
+ {
83
95
return s1.top ();
84
96
}
85
97
86
- int getMin () {
98
+ int getMin ()
99
+ {
87
100
return s2.top ();
88
101
}
89
102
}
@@ -97,14 +110,18 @@ class MinStack {
97
110
* int param_4 = obj.getMin();
98
111
*/
99
112
100
- int main () {
113
+ int
114
+ main ()
115
+ {
101
116
MyStack s;
102
117
s.push (1 );
103
118
s.push (2 );
104
119
s.push (3 );
105
120
106
- for (int i = 0 ; i < 4 ; ++i) {
107
- if (!s.isEmpty ()) {
121
+ for (int i = 0 ; i < 4 ; ++i)
122
+ {
123
+ if (!s.isEmpty ())
124
+ {
108
125
cout << s.top () << endl;
109
126
}
110
127
cout << (s.pop () ? " true" : " false" ) << endl;
0 commit comments