解决Next Greater Number问题
4.计算器相关


function calculate(s){ let list = s.split(''); function helper(list){ let stack=[],num=0,sign='+'; while(list.length){ const c = list.pop(); if(!isNaN(c)){ num = num*10+(c-'0'); } if(c==='('){ num = helper(list) } if(c!===' '&&(c==='+' || c==='-' || c==='*' || c==='/') || !list.length){ if(sign==='+'){ stack.push(num) }else if(sign==='-'){ stack.push(-num) }else if(sign==='*'){ const top = stack.pop(); const temp = top*num; stack.push(temp) }else if(sign==='/'){ const top = stack.pop(); const temp = Math.floor(top/num); stack.push(temp) } sign = c; num=0; } if(c===')'){ break; } } return stack.reduce((pre,next)=>pre+next,0); } return helper(list);}