今日学习的内容
● 20. 有效的括号
var isValid = function(s) {
let stack = [] ;
for(let i = 0 ;i < s.length ; i++){
let temp = s[i];
if(temp == '('){
stack.push(')')
continue;
}
if(temp == '['){
stack.push(']');
continue;
}
if(temp == '{'){
stack.push('}')
}else{
if(temp != stack.pop()){
return false
}
}
}
console.log(stack,90)
return stack.length ==0
};
● 1047. 删除字符串中的所有相邻重复项
var removeDuplicates = function(s) {
let stack = [] ;
s = Array.from(s);
for(let i = 0 ;i < s.length ;i++){
if(stack[stack.length-1] == s[i]){
stack.pop()
}else{
stack.push(s[i])
}
}
return stack.join('')
};
● 150. 逆波兰表达式求值
var evalRPN = function(tokens) {
let stack = [] ;
let obj = {
'+':1,
'-':1,
'*':1,
'/':1
}
let res =0
for(let i = 0 ; i < tokens.length ; i++){
if(obj[tokens[i]]){
let a = stack.pop();
let b = stack.pop();
if(tokens[i] == '+'){
res = a/1+b/1
}
if(tokens[i] == '-'){
res = b/1-a/1
}
if(tokens[i] == '*'){
res = a*b
}
if(tokens[i] == '/'){
console.log(a,b)
res = b/a|0
// res = Math.floor(b*1/a) ||0
}
stack.push(res)
}else{
stack.push(tokens[i])
}
console.log(stack)
}
// console.log(stack)
return stack.pop()
};
今日收获,记录一下自己的学习时长
标签:第十一天,res,随想录,pop,tokens,push,let,求值,stack From: https://www.cnblogs.com/lijiacheng/p/17842845.html1.5h