mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-01 00:08:53 +00:00
leetcode 225 补充js版解法
This commit is contained in:
parent
39683f1794
commit
4e53da9e3e
@ -18,6 +18,7 @@
|
||||
|
||||
下面我们来看一下题目代码,也是很容易理解。
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class MyStack {
|
||||
//初始化队列
|
||||
@ -55,3 +56,34 @@ class MyStack {
|
||||
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var MyStack = function() {
|
||||
this.queue = [];
|
||||
};
|
||||
|
||||
MyStack.prototype.push = function(x) {
|
||||
this.queue.push(x);
|
||||
if (this.queue.length > 1) {
|
||||
let i = this.queue.length - 1;
|
||||
while (i) {
|
||||
this.queue.push(this.queue.shift());
|
||||
i--;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MyStack.prototype.pop = function() {
|
||||
return this.queue.shift();
|
||||
};
|
||||
|
||||
MyStack.prototype.top = function() {
|
||||
return this.empty() ? null : this.queue[0];
|
||||
|
||||
};
|
||||
|
||||
MyStack.prototype.empty = function() {
|
||||
return !this.queue.length;
|
||||
};
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user