124. calculate arithmetic expression

JavaScriptAlgorithm
Is this helpful ?

medium  - accepted / - tried

In 119. create a tokenizer, you are able to extract the tokens from a string with invalid spaces.

Now please calculate() the result of the string. You can use the tokenizer you wrote before.


calculate('1 * (20 -   300      ) ')
// -280

calculate('     1/0 ')
// Infinity
  1. the input expression is syntactically valid, containing non-negative integers, +, -, *, /, (, ) and spaces
  2. Don't use eval()

Let's try to solve this problem within 30 minutes.

Discuss(11)