mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #263 from flames519/master
0232,0225,0020,1047,0150JavaScript版本
This commit is contained in:
@ -271,6 +271,23 @@ var isValid = function (s) {
|
||||
}
|
||||
return stack.length === 0;
|
||||
};
|
||||
// 简化版本
|
||||
var isValid = function(s) {
|
||||
const stack = [],
|
||||
map = {
|
||||
"(":")",
|
||||
"{":"}",
|
||||
"[":"]"
|
||||
};
|
||||
for(const x of s) {
|
||||
if(x in map) {
|
||||
stack.push(x);
|
||||
continue;
|
||||
};
|
||||
if(map[stack.pop()] !== x) return false;
|
||||
}
|
||||
return !stack.length;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
@ -197,6 +197,32 @@ func evalRPN(tokens []string) int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
|
||||
/**
|
||||
* @param {string[]} tokens
|
||||
* @return {number}
|
||||
*/
|
||||
var evalRPN = function(tokens) {
|
||||
const s = new Map([
|
||||
["+", (a, b) => a * 1 + b * 1],
|
||||
["-", (a, b) => b - a],
|
||||
["*", (a, b) => b * a],
|
||||
["/", (a, b) => (b / a) | 0]
|
||||
]);
|
||||
const stack = [];
|
||||
for (const i of tokens) {
|
||||
if(!s.has(i)) {
|
||||
stack.push(i);
|
||||
continue;
|
||||
}
|
||||
stack.push(s.get(i)(stack.pop(),stack.pop()))
|
||||
}
|
||||
return stack.pop();
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -357,8 +357,115 @@ class MyStack:
|
||||
|
||||
Go:
|
||||
|
||||
javaScript:
|
||||
|
||||
使用数组(push, shift)模拟队列
|
||||
|
||||
```js
|
||||
|
||||
// 使用两个队列实现
|
||||
/**
|
||||
* Initialize your data structure here.
|
||||
*/
|
||||
var MyStack = function() {
|
||||
this.queue1 = [];
|
||||
this.queue2 = [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Push element x onto stack.
|
||||
* @param {number} x
|
||||
* @return {void}
|
||||
*/
|
||||
MyStack.prototype.push = function(x) {
|
||||
this.queue1.push(x);
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the element on top of the stack and returns that element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyStack.prototype.pop = function() {
|
||||
// 减少两个队列交换的次数, 只有当queue1为空时,交换两个队列
|
||||
if(!this.queue1.length) {
|
||||
[this.queue1, this.queue2] = [this.queue2, this.queue1];
|
||||
}
|
||||
while(this.queue1.length > 1) {
|
||||
this.queue2.push(this.queue1.shift());
|
||||
}
|
||||
return this.queue1.shift();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the top element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyStack.prototype.top = function() {
|
||||
const x = this.pop();
|
||||
this.queue1.push(x);
|
||||
return x;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns whether the stack is empty.
|
||||
* @return {boolean}
|
||||
*/
|
||||
MyStack.prototype.empty = function() {
|
||||
return !this.queue1.length && !this.queue2.length;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
// 使用一个队列实现
|
||||
/**
|
||||
* Initialize your data structure here.
|
||||
*/
|
||||
var MyStack = function() {
|
||||
this.queue = [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Push element x onto stack.
|
||||
* @param {number} x
|
||||
* @return {void}
|
||||
*/
|
||||
MyStack.prototype.push = function(x) {
|
||||
this.queue.push(x);
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the element on top of the stack and returns that element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyStack.prototype.pop = function() {
|
||||
let size = this.queue.length;
|
||||
while(size-- > 1) {
|
||||
this.queue.push(this.queue.shift());
|
||||
}
|
||||
return this.queue.shift();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the top element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyStack.prototype.top = function() {
|
||||
const x = this.pop();
|
||||
this.queue.push(x);
|
||||
return x;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns whether the stack is empty.
|
||||
* @return {boolean}
|
||||
*/
|
||||
MyStack.prototype.empty = function() {
|
||||
return !this.queue.length;
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
@ -353,6 +353,61 @@ func (this *MyQueue) Empty() bool {
|
||||
*/
|
||||
```
|
||||
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
// 使用两个数组的栈方法(push, pop) 实现队列
|
||||
/**
|
||||
* Initialize your data structure here.
|
||||
*/
|
||||
var MyQueue = function() {
|
||||
this.stack1 = [];
|
||||
this.stack2 = [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Push element x to the back of queue.
|
||||
* @param {number} x
|
||||
* @return {void}
|
||||
*/
|
||||
MyQueue.prototype.push = function(x) {
|
||||
this.stack1.push(x);
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the element from in front of queue and returns that element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyQueue.prototype.pop = function() {
|
||||
const size = this.stack2.length;
|
||||
if(size) {
|
||||
return this.stack2.pop();
|
||||
}
|
||||
while(this.stack1.length) {
|
||||
this.stack2.push(this.stack1.pop());
|
||||
}
|
||||
return this.stack2.pop();
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the front element.
|
||||
* @return {number}
|
||||
*/
|
||||
MyQueue.prototype.peek = function() {
|
||||
const x = this.pop();
|
||||
this.stack2.push(x);
|
||||
return x;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns whether the queue is empty.
|
||||
* @return {boolean}
|
||||
*/
|
||||
MyQueue.prototype.empty = function() {
|
||||
return !this.stack1.length && !this.stack2.length
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -186,6 +186,24 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
javaScript:
|
||||
|
||||
```js
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {string}
|
||||
*/
|
||||
var removeDuplicates = function(s) {
|
||||
const stack = [];
|
||||
for(const x of s) {
|
||||
let c = null;
|
||||
if(stack.length && x === (c = stack.pop())) continue;
|
||||
c && stack.push(c);
|
||||
stack.push(x);
|
||||
}
|
||||
return stack.join("");
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user