添加(0232.用栈实现队列.md):增加typescript版本

This commit is contained in:
Steve2020
2022-01-21 14:16:33 +08:00
parent 451fbe16f6
commit d29fa29a87

View File

@ -348,7 +348,44 @@ MyQueue.prototype.empty = function() {
};
```
TypeScript:
```typescript
class MyQueue {
private stackIn: number[]
private stackOut: number[]
constructor() {
this.stackIn = [];
this.stackOut = [];
}
push(x: number): void {
this.stackIn.push(x);
}
pop(): number {
if (this.stackOut.length === 0) {
while (this.stackIn.length > 0) {
this.stackOut.push(this.stackIn.pop()!);
}
}
return this.stackOut.pop()!;
}
peek(): number {
let temp: number = this.pop();
this.stackOut.push(temp);
return temp;
}
empty(): boolean {
return this.stackIn.length === 0 && this.stackOut.length === 0;
}
}
```
Swift
```swift
class MyQueue {