/** * @param {string} s * @return {number} */ var calculate = function(s) { s = s.replace(/\s/g, ''); var numStack = []; var symStack = []; var len = s.length; var num = 0;
for (var i = 0; i < len; i++) { var item = s[i]; if ('-+('.indexOf(item) !== -1) { symStack.push(item);
if (i && '0123456789'.indexOf(s[i - 1]) !== -1) { numStack.push(num); num = 0; } } elseif (item === ')') { if (i && '0123456789'.indexOf(s[i - 1]) !== -1) { numStack.push(num); num = 0; }
var _numStack = []; var _symStack = [];
while (true) { var sym = symStack.pop(); if (sym === '(') { _numStack.unshift(numStack.pop()); numStack.push(help(_numStack, _symStack)); break; }
_numStack.unshift(numStack.pop()); _symStack.unshift(sym); } } else { num = num * 10 + (+item); } }
if (s[len - 1] !== ')') numStack.push(num);
return help(numStack, symStack); };
/** * @param {array} numStack * @param {array} symStack * @return {number} */ functionhelp(numStack, symStack) { while (numStack.length !== 1) { var sym = symStack.shift(); var a = numStack.shift(); var b = numStack.shift();
/** * @param {string} s * @return {number} */ var calculate = function(s) { var ans = 0; var num = 0; // single number var sign = 1; var numStack = []; var symStack = [];
for (var i = 0, len = s.length; i < len; i++) { var item = s[i];
if ('0123456789'.indexOf(item) !== -1) { num = num * 10 + (+item); } else { ans += sign * num; num = 0; if (item === '+') sign = 1; elseif (item === '-') sign = -1; elseif (item === '(') { numStack.push(ans); symStack.push(sign); ans = 0; sign = 1; } elseif (item === ')') { ans = symStack.pop() * ans + numStack.pop(); } } }