227. Basic Calculator II

来源:互联网 发布:ds数据精灵激活码 编辑:程序博客网 时间:2024/05/29 06:40

227. 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.

    You may assume that the given expression is always valid.

    Some examples:

    "3+2*2" = 7" 3/2 " = 1" 3+5 / 2 " = 5
  • 题目大意:实现加减乘除基本计算

  • 代码

    package String;import java.util.Stack;/*** @Author OovEver* @Date 2017/12/10 16:20*/public class LeetCode227 {      public static int calculate(String s) {          int len = 0;          if (s == null || (len = s.length()) == 0) {              return 0;          }          Stack<Integer> stack = new Stack<>();          char sign = '+';          int num = 0;          for(int i=0;i<s.length();i++) {              if (Character.isDigit(s.charAt(i))) {                  num = num * 10 + s.charAt(i) - '0';              }              if (!Character.isDigit(s.charAt(i)) && ' ' != s.charAt(i) || i == len - 1) {                  switch (sign) {                      case '+':                          stack.push(num);                          break;                      case '-':                          stack.push(-num);                          break;                      case '*':                          stack.push(stack.pop()*num);                          break;                      case '/':                          stack.push(stack.pop()/num);                          break;                  }                  sign = s.charAt(i);                  num = 0;              }          }          int re = 0;          for(int i:stack){              re += i;          }          return re;      }  public static void main(String[] args) {      System.out.println(calculate("1"));  }}