mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1046 from xiaofei-2020/stack2
添加(0232.用栈实现队列.md):增加typescript版本
This commit is contained in:
@ -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 {
|
||||
|
||||
|
Reference in New Issue
Block a user