mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加(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:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class MyQueue {
|
class MyQueue {
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user