diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 315ff384..dae84354 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -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; +}; ``` diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index 618a6830..e38bc1a3 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -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(); +}; +``` ----------------------- diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index e045229f..055c2e3d 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -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) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 7c91a0dd..10314a08 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -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 +}; + ``` + diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 8b761be7..9ca08c96 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -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(""); +}; +```