#227

Basic Calculator II

specialist · 930 · lc medium +32 · verified · 46.7% accepted · 6,590 likes · top 31%

play →

Description

A string s encodes an arithmetic expression. Evaluate it and return the integer result. Integer division must truncate toward zero.

The expression is always valid, and all intermediate values fit in [-231, 231 - 1].

Note: You may not use any built-in function that evaluates strings as code (e.g., eval()).

Example 1:

Input: s = "3+2*2"
Output: 7

Example 2:

Input: s = " 3/2 "
Output: 1

Example 3:

Input: s = " 3+5 / 2 "
Output: 5

Code

1
2
3