Merge pull request #1046 from xiaofei-2020/stack2

添加(0232.用栈实现队列.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-02-08 10:44:37 +08:00
committed by GitHub

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 {