File tree 3 files changed +124
-0
lines changed
lcci/03.05.Sort of Stacks
3 files changed +124
-0
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,49 @@ class SortedStack {
115
115
*/
116
116
```
117
117
118
+ ### ** TypeScript**
119
+
120
+ ``` ts
121
+ class SortedStack {
122
+ stack: number [];
123
+ constructor () {
124
+ this .stack = [];
125
+ }
126
+
127
+ push(val : number ): void {
128
+ let t = [];
129
+ while (! this .isEmpty () && this .peek () < val ) {
130
+ t .push (this .stack .pop ());
131
+ }
132
+ this .stack .push (val );
133
+ while (t .length > 0 ) {
134
+ this .stack .push (t .pop ());
135
+ }
136
+ }
137
+
138
+ pop(): void {
139
+ this .stack .pop ();
140
+ }
141
+
142
+ peek(): number {
143
+ return this .isEmpty () ? - 1 : this .stack [this .stack .length - 1 ];
144
+ }
145
+
146
+ isEmpty(): boolean {
147
+ return this .stack .length == 0 ;
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Your SortedStack object will be instantiated and called as such:
153
+ * var obj = new SortedStack()
154
+ * obj.push(val)
155
+ * obj.pop()
156
+ * var param_3 = obj.peek()
157
+ * var param_4 = obj.isEmpty()
158
+ */
159
+ ```
160
+
118
161
### ** Go**
119
162
120
163
``` go
Original file line number Diff line number Diff line change @@ -120,6 +120,49 @@ class SortedStack {
120
120
*/
121
121
```
122
122
123
+ ### ** TypeScript**
124
+
125
+ ``` ts
126
+ class SortedStack {
127
+ stack: number [];
128
+ constructor () {
129
+ this .stack = [];
130
+ }
131
+
132
+ push(val : number ): void {
133
+ let t = [];
134
+ while (! this .isEmpty () && this .peek () < val ) {
135
+ t .push (this .stack .pop ());
136
+ }
137
+ this .stack .push (val );
138
+ while (t .length > 0 ) {
139
+ this .stack .push (t .pop ());
140
+ }
141
+ }
142
+
143
+ pop(): void {
144
+ this .stack .pop ();
145
+ }
146
+
147
+ peek(): number {
148
+ return this .isEmpty () ? - 1 : this .stack [this .stack .length - 1 ];
149
+ }
150
+
151
+ isEmpty(): boolean {
152
+ return this .stack .length == 0 ;
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Your SortedStack object will be instantiated and called as such:
158
+ * var obj = new SortedStack()
159
+ * obj.push(val)
160
+ * obj.pop()
161
+ * var param_3 = obj.peek()
162
+ * var param_4 = obj.isEmpty()
163
+ */
164
+ ```
165
+
123
166
### ** Go**
124
167
125
168
``` go
Original file line number Diff line number Diff line change
1
+ class SortedStack {
2
+ stack : number [ ] ;
3
+ constructor ( ) {
4
+ this . stack = [ ] ;
5
+ }
6
+
7
+ push ( val : number ) : void {
8
+ let t = [ ] ;
9
+ while ( ! this . isEmpty ( ) && this . peek ( ) < val ) {
10
+ t . push ( this . stack . pop ( ) ) ;
11
+ }
12
+ this . stack . push ( val ) ;
13
+ while ( t . length > 0 ) {
14
+ this . stack . push ( t . pop ( ) ) ;
15
+ }
16
+ }
17
+
18
+ pop ( ) : void {
19
+ this . stack . pop ( ) ;
20
+ }
21
+
22
+ peek ( ) : number {
23
+ return this . isEmpty ( ) ? - 1 : this . stack [ this . stack . length - 1 ] ;
24
+ }
25
+
26
+ isEmpty ( ) : boolean {
27
+ return this . stack . length == 0 ;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Your SortedStack object will be instantiated and called as such:
33
+ * var obj = new SortedStack()
34
+ * obj.push(val)
35
+ * obj.pop()
36
+ * var param_3 = obj.peek()
37
+ * var param_4 = obj.isEmpty()
38
+ */
You can’t perform that action at this time.
0 commit comments