227. Basic Calculator II

227. Basic Calculator II

  
class Solution {
    public int calculate(String s) {
      // remove all empty space 
      s = s.replaceAll(" ", "");
      int sum = s.charAt(0) - '0';
      int prev = s.charAt(0) - '0';
      int index = 0;
      char op = '+';
      

      // more than one digits 
      while(index + 1 < s.length() && Character.isDigit(s.charAt(index+1))){
        sum = sum * 10 + (s.charAt(index + 1) - '0');
        prev = prev * 10 + (s.charAt(index + 1) - '0');
        index++;
      }

    
      
      for(int i = index + 1; i < s.length(); i++){
        char c = s.charAt(i);
        if(!Character.isDigit(c)){
          op = c;
        }else{
          int current = c - '0';
          while(i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))){
            current = current * 10 + (s.charAt(i + 1) - '0'); // 要减 0, 不减0, 就是 一个 char。 
            i++;
          }
          if(op == '+'){
            sum = sum + current;
            prev = current;
          }
          if(op == '-'){
            sum = sum - current;
            prev = -current;
          }
          if(op == '*'){
            sum = sum - prev + prev * current;
            prev = prev * current;
          }
          if(op == '/'){
            sum = sum - prev + prev / current;
            prev = prev / current;
          }
        }
      }
      return sum;
    }
}

 

we have a string, can look like 32 + 2 * 46 

And we are not sure if there is any white space anywhere in the string 

So we use s = s.replaceAll(" ", "") to remove all the white space

 

First we need to get the first number

First we need to get the first digit 

And use a var called prev , which is needed when we do / and * 

 

Need to use int var index to check if the next char is digit or not 

need to use a char var op to record which operation we are dealing  with right now

 

Okay, finally we need to get the first number in the string , 

But we are not sure which digit the first number has, so we use index + 1 to check if the 

Next digit is a digit, if it is a digit, then 

 

For example, 32 + 2 * 46 

The first digit is 3, and the next digit is 2, 

We have 3 * 10 + 2 = 32 

Prev is the same when we encounters the first number 

 

 

So we have prev = 32 and sum = 32 

 

==============================

Now we can do the normal operation 

First, check the current index is a digit or not a digit, 

If it’s not a digit, then it’s a op sign, keep it 

And do the next iteration 

 

If its a digit, we need to get all the digits in the current number 

 

Need to use a int var called current to get the current number 

 

The four operations, update sum and prev , because we might encounter * and / later, 

So we need to keep prev, 

 

After i reaches the end of length, we are done 

Return sum 

 

 

 

 

 

Basic Calculator II  (只有加和乘 )

(面经是  计算器 只有+-×/, 没有括号, ×/优先级高)

 

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +-*/ operators and empty spaces . The integer division should truncate toward zero.

Example 1:

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

Example 2:

Input: " 3/2 "
Output: 1

Example 3:

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

posted on 2018-08-10 15:00  猪猪&#128055;  阅读(134)  评论(0)    收藏  举报

导航