Skip to content

Commit 4b977a6

Browse files
authored
feat: add typescript solution to locf problem: No.03.05.Sort of Stacks (#514)
1 parent c1dd1a5 commit 4b977a6

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

lcci/03.05.Sort of Stacks/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,49 @@ class SortedStack {
115115
*/
116116
```
117117

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+
118161
### **Go**
119162

120163
```go

lcci/03.05.Sort of Stacks/README_EN.md

+43
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,49 @@ class SortedStack {
120120
*/
121121
```
122122

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+
123166
### **Go**
124167

125168
```go

lcci/03.05.Sort of Stacks/Solution.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
*/

0 commit comments

Comments
 (0)