-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path227. Basic Calculator II.ts
48 lines (43 loc) · 1.21 KB
/
227. Basic Calculator II.ts
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
/**
* Runtime 189 ms Beats 15.7%
* Memory 73.8 MB Beats 8.22%
*/
function calculate(s: string): number {
const strArray = s
.replace(/ /g, '')
.replace(/[-+*\/]/g, (s) => ` ${s} `)
.split(' ');
const multipliedAndDivided: (string | number)[] = [];
for (let i = 0; i < strArray.length; i++) {
if (/[0-9+-]/.test(strArray[i])) {
multipliedAndDivided.push(strArray[i]);
} else {
const num1 = multipliedAndDivided.pop();
const num2 = strArray[i + 1];
if (strArray[i] === '*') {
multipliedAndDivided.push(Number(num1) * Number(num2));
} else {
multipliedAndDivided.push(Math.floor(Number(num1) / Number(num2)));
}
i++;
}
}
let result = Number(multipliedAndDivided[0]);
let operator = '+';
for (let i = 1; i < multipliedAndDivided.length; i++) {
if (multipliedAndDivided[i] === '+') {
operator = '+';
continue;
} else if (multipliedAndDivided[i] === '-') {
operator = '-';
continue;
}
if (operator === '+') {
result += Number(multipliedAndDivided[i]);
} else {
result -= Number(multipliedAndDivided[i]);
}
}
return result;
}
console.log(calculate('42 + 2234512 / 2 ')); // 1117298