var MyQueue = function () {
this.stackIn = []
this.stackOut = []
this.size = 0
};
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function (x) {
this.stackIn.push(x)
this.size++
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function () {
this.size--
if (this.stackOut.length > 0) {
return this.stackOut.pop()
} else {
while (this.stackIn.length > 0) {
this.stackOut.push(this.stackIn.pop())
}
return this.stackOut.pop()
}
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function () {
if (this.stackOut.length > 0) {
return this.stackOut[this.stackOut.length - 1]
} else {
return this.stackIn[0]
}
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function () {
return this.size === 0
};
/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/
var MyStack = function () {
this.queue = []
this.size = 0
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function (x) {
this.queue.push(x)
this.size++
};
/**
* @return {number}
*/
MyStack.prototype.pop = function () {
for (let i = 0; i < this.size - 1; i++) {
this.queue.push(this.queue.shift())
}
this.size--
return this.queue.shift()
};
/**
* @return {number}
*/
MyStack.prototype.top = function () {
return this.queue[this.size - 1]
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function () {
return this.size === 0
};
/**
* Your MyStack object will be instantiated and called as such:
* var obj = new MyStack()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const stack = []
for (let i = 0; i < s.length; i++) {
if (s[i] === '(' || s[i] === '[' || s[i] === '{') {
stack.push(s[i])
} else if (s[i] === ')') {
if (stack[stack.length - 1] === '(') {
stack.pop()
} else {
return false
}
} else if (s[i] === ']') {
if (stack[stack.length - 1] === '[') {
stack.pop()
} else {
return false
}
} else if (s[i] === '}') {
if (stack[stack.length - 1] === '{') {
stack.pop()
} else {
return false
}
}
}
return stack.length === 0
};
1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
/**
* @param {string} s
* @return {string}
*/
var removeDuplicates = function (s) {
const stack = [s[0]]
for (let i = 1; i < s.length; i++) {
if (s[i] === stack[stack.length - 1]) {
stack.pop()
} else {
stack.push(s[i])
}
}
return stack.join('')
};
/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens) {
const stack = []
for (let i = 0; i < tokens.length; i++) {
if (tokens[i] !== '+' && tokens[i] !== '-' && tokens[i] !== '*' && tokens[i] !== '/') {
stack.push(tokens[i])
} else {
const num1 = Number(stack.pop())
const num2 = Number(stack.pop())
if (tokens[i] === '+') {
stack.push(num2 + num1)
} else if (tokens[i] === '-') {
stack.push(num2 - num1)
} else if (tokens[i] === '*') {
stack.push(num2 * num1)
} else if (tokens[i] === '/') {
stack.push(num2 / num1 | 0)
}
}
}
return stack[0]
};
/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens) {
const stack = []
for (let i = 0; i < tokens.length; i++) {
if (tokens[i] !== '+' && tokens[i] !== '-' && tokens[i] !== '*' && tokens[i] !== '/') {
stack.push(tokens[i])
} else {
const num1 = Number(stack.pop())
const num2 = Number(stack.pop())
if (tokens[i] === '+') {
stack.push(num2 + num1)
} else if (tokens[i] === '-') {
stack.push(num2 - num1)
} else if (tokens[i] === '*') {
stack.push(num2 * num1)
} else if (tokens[i] === '/') {
stack.push(num2 / num1 | 0)
}
}
}
return stack[0]
};