有效括号问题
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
1、左括号必须用相同类型的右括号闭合。
2、左括号必须以正确的顺序闭合。
3、注意空字符串可被认为是有效字符串。
输入: "()" 输出: true
输入: "()[]{}" 输出: true
const leftToRight = {
"(": ")",
"[": "]",
"{": "}"
};
const isValid = function(s) {
if (!s) {
return true;
}
const stack = [];
const len = s.length;
for (let i = 0; i < len; i++) {
const ch = s[i];
if (ch === "(" || ch === "{" || ch === "[") stack.push(leftToRight[ch]);
else {
if (!stack.length || stack.pop() !== ch) {
return false;
}
}
}
return !stack.length;
};
每日温度问题
根据每日气温列表,请重新生成一个列表,对应位置的输出是需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。
例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。
const dailyTemperatures = function(T) {
const len = T.length
const stack = []
const res = (new Array(len)).fill(0)
for(let i=0;i<len;i++) {
while(stack.length && T[i] > T[stack[stack.length-1]]) {
const top = stack.pop()
res[top] = i - top
}
stack.push(i)
}
return res
};
“最小栈”问题
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
const MinStack = function() {
this.stack = []
};
MinStack.prototype.push = function(x) {
this.stack.push(x)
};
MinStack.prototype.pop = function() {
this.stack.pop()
};
MinStack.prototype.top = function() {
if(!this.stack || !this.stack.length) {
return
}
return this.stack[this.stack.length - 1]
};
MinStack.prototype.getMin = function() {
let minValue = Infinity
const { stack } = this
for(let i=0; i<stack.length;i++) {
if(stack[i] < minValue) {
minValue = stack[i]
}
}
return minValue
};
时间效率更高的做法
const MinStack = function() {
this.stack = [];
this.stack2 = [];
};
MinStack.prototype.push = function(x) {
this.stack.push(x);
if(this.stack2.length == 0 || this.stack2[this.stack2.length-1] >= x){
this.stack2.push(x);
}
};
MinStack.prototype.pop = function() {
if(this.stack.pop() == this.stack2[this.stack2.length-1]){
this.stack2.pop();
}
};
MinStack.prototype.top = function() {
return this.stack[this.stack.length-1];
};
MinStack.prototype.getMin = function() {
return this.stack2[this.stack2.length-1];
};