Yu's Coding Garden : leetcode Question: Basic Calculator II
Monday, October 26, 2015 Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, + , - , * , / You may assume that the given expression is always valid. Some examples: "3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5 Analysis: This is a straightforward question that only requires basic string operations. Note that, there is no brackets "(" or ")" in the string, there is no negative numbers in the string. All we have is a string with +, -, *, / and numbers, and spaces. Note that, the numbers in the string is not constrained to be 0 to 9, in other words, the first step is to split those numbers and operators from the string. We can keep every digits of the number in a temporary string, until meet one of the operators. Then convert the number string into int and save it, don't forget to reset the tmp string. Now we have a list of numbers as well as a list of operators.Read full article from Yu's Coding Garden : leetcode Question: Basic Calculator II
No comments:
Post a Comment