File tree 3 files changed +115
-0
lines changed
3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,46 @@ class MinStack {
97
97
*/
98
98
```
99
99
100
+ ### ** TypeScript**
101
+
102
+ ``` ts
103
+ class MinStack {
104
+ stack: number [];
105
+ mins: number [];
106
+ constructor () {
107
+ this .stack = [];
108
+ this .mins = [];
109
+ }
110
+
111
+ push(x : number ): void {
112
+ this .stack .push (x );
113
+ this .mins .push (Math .min (this .getMin (), x ));
114
+ }
115
+
116
+ pop(): void {
117
+ this .stack .pop ();
118
+ this .mins .pop ();
119
+ }
120
+
121
+ top(): number {
122
+ return this .stack [this .stack .length - 1 ];
123
+ }
124
+
125
+ getMin(): number {
126
+ return this .mins .length == 0 ? Infinity : this .mins [this .mins .length - 1 ];
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Your MinStack object will be instantiated and called as such:
132
+ * var obj = new MinStack()
133
+ * obj.push(x)
134
+ * obj.pop()
135
+ * var param_3 = obj.top()
136
+ * var param_4 = obj.getMin()
137
+ */
138
+ ```
139
+
100
140
### ** ...**
101
141
102
142
```
Original file line number Diff line number Diff line change @@ -108,6 +108,46 @@ class MinStack {
108
108
*/
109
109
```
110
110
111
+ ### ** TypeScript**
112
+
113
+ ``` ts
114
+ class MinStack {
115
+ stack: number [];
116
+ mins: number [];
117
+ constructor () {
118
+ this .stack = [];
119
+ this .mins = [];
120
+ }
121
+
122
+ push(x : number ): void {
123
+ this .stack .push (x );
124
+ this .mins .push (Math .min (this .getMin (), x ));
125
+ }
126
+
127
+ pop(): void {
128
+ this .stack .pop ();
129
+ this .mins .pop ();
130
+ }
131
+
132
+ top(): number {
133
+ return this .stack [this .stack .length - 1 ];
134
+ }
135
+
136
+ getMin(): number {
137
+ return this .mins .length == 0 ? Infinity : this .mins [this .mins .length - 1 ];
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Your MinStack object will be instantiated and called as such:
143
+ * var obj = new MinStack()
144
+ * obj.push(x)
145
+ * obj.pop()
146
+ * var param_3 = obj.top()
147
+ * var param_4 = obj.getMin()
148
+ */
149
+ ```
150
+
111
151
### ** ...**
112
152
113
153
```
Original file line number Diff line number Diff line change
1
+ class MinStack {
2
+ stack : number [ ] ;
3
+ mins : number [ ] ;
4
+ constructor ( ) {
5
+ this . stack = [ ] ;
6
+ this . mins = [ ] ;
7
+ }
8
+
9
+ push ( x : number ) : void {
10
+ this . stack . push ( x ) ;
11
+ this . mins . push ( Math . min ( this . getMin ( ) , x ) ) ;
12
+ }
13
+
14
+ pop ( ) : void {
15
+ this . stack . pop ( ) ;
16
+ this . mins . pop ( ) ;
17
+ }
18
+
19
+ top ( ) : number {
20
+ return this . stack [ this . stack . length - 1 ] ;
21
+ }
22
+
23
+ getMin ( ) : number {
24
+ return this . mins . length == 0 ? Infinity : this . mins [ this . mins . length - 1 ] ;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Your MinStack object will be instantiated and called as such:
30
+ * var obj = new MinStack()
31
+ * obj.push(x)
32
+ * obj.pop()
33
+ * var param_3 = obj.top()
34
+ * var param_4 = obj.getMin()
35
+ */
You can’t perform that action at this time.
0 commit comments