-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicCalculator.js
More file actions
91 lines (80 loc) · 2.66 KB
/
Copy pathbasicCalculator.js
File metadata and controls
91 lines (80 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
*
* Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
* @param {string} s
* @return {number}
*/
var calculate = function (s) {
return (function recursive(start, end) {
const stackOfParenthesisIndices = [];
const stackOfNumbers = [];
const opening = '(';
const closing = ')';
let sign = 1;
for (let i = start; i < end; i += 1) {
if (s[i] === opening) {
stackOfParenthesisIndices.push(i);
} else if (s[i] === closing) {
let start = stackOfParenthesisIndices.pop();
if (stackOfParenthesisIndices.length === 0) {
let sub = recursive(start + 1, i);
stackOfNumbers.push(sub * sign);
sign = 1;
}
} else if (stackOfParenthesisIndices.length === 0) {
if (s[i] !== ' ' && !isNaN(+s[i])) {
let numString = '';
while (!isNaN(+s[i])) {
numString += s[i];
i += 1;
}
stackOfNumbers.push(parseInt(numString) * sign);
sign = 1;
i -= 1;
} else if (s[i] === '-') {
sign = -1;
} else if (s[i] === '+') {
sign = 1;
}
}
}
const subres = stackOfNumbers.reduce((soFar, num) => soFar += +num, 0);
return subres;
})(0, s.length);
};
/*
BETTER SOLUTION
var calculate = function(s) {
var res = 0, sign = 1, n = s.length, st = [];
for (var i = 0; i < n; ++i) {
if (s[i] >= '0'&& s[i] <= '9') {
var num = 0;
while (i < n && s[i] >= '0'&& s[i] <= '9') {
num = 10 * num + parseInt(s[i++]);
}
res += sign * num;
--i;
} else if (s[i] == '+') {
sign = 1;
} else if (s[i] == '-') {
sign = -1;
} else if (s[i] == '(') {
st.push(res);
st.push(sign);
res = 0;
sign = 1;
} else if (s[i] == ')') {
res *= st.pop();
res += st.pop();
}
}
return res;
};
*/