[Leetcode] 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
Note: Do not use the eval
built-in library function.
这个题目需要实现+、-、*、\ 四种运算,实际上思想是一致,你是否可以首先更多运算符,比如 log,cos,sin,sqrt等等。
这里有一个问题需要注意一下:
当遇到+或者-的时候,显然栈顶的任何操作符都可以拿出来进行运算,因为至少优先级是一样的,并且可以确定,在占中的所有运算都是可以拿出来跑的,那么这个时候我们是将其全部拿出全部处理呢?还是只处理一个?
case '+': case '-': if(!op.isEmpty()){ char opthis = op.pop(); int data1 = data.pop(); int data2 = data.pop(); int res = 0; if(opthis=='+') res = data1+data2; else if(opthis == '-') res = data2 -data1; else if(opthis == '*') res = data1 *data2; else res = data2/data1; data.push(res); } op.push(opc);
如果只处理一个会出现问题,关键原因是减号的计算需要具有有序性,比如
2-3+4 = 3如果这是放弃对减号的有限计算,就会变成
2-(3+4)=-5,显然错误。
其他地方也没有什么好说的了
1 import java.util.*; 2 3 public class Solution { 4 private Vector<String> getSeg(String s){ 5 int index = 0; 6 Vector<String> res = new Vector<String>(); 7 while(index<s.length()){ 8 char c = s.charAt(index); 9 if(Character.isDigit(c)){ 10 int endindex = index+1; 11 while(endindex<s.length()&&Character.isDigit(s.charAt(endindex))) endindex++; 12 String seg = s.substring(index,endindex); 13 res.add(seg); 14 index = endindex; 15 }else{ 16 if(c==' '){} 17 else{ 18 String seg = s.substring(index,index+1); 19 res.add(seg); 20 } 21 index++; 22 } 23 } 24 return res; 25 } 26 public int calculate(String s) { 27 Vector<String> segments = getSeg(s); 28 Stack<Integer> data = new Stack<Integer>(); 29 Stack<Character> op = new Stack<Character>(); 30 for(int i=0;i<segments.size();i++){ 31 String segone = segments.get(i); 32 char opc = segone.charAt(0); 33 if(Character.isDigit(opc)){ 34 data.push(Integer.parseInt(segone)); 35 }else{ 36 switch(opc){ 37 case '+': 38 case '-'://如果把下面的代码改为if语句就是错误的,应该在某些情况下,将栈中所有优先级第高的操作符全部处理完毕 39 //不然高优先级的操作可能会被低优先级的操作枪战 40 //例如1 - 2*3 +5 41 //如果遇到+时,前面的减号不一起计算的话,会导致最后的结果是-10,明显错误 42 while(!op.isEmpty()){ 43 char opthis = op.pop(); 44 int data1 = data.pop(); 45 int data2 = data.pop(); 46 int res = 0; 47 if(opthis=='+') res = data1+data2; 48 else if(opthis == '-') res = data2 -data1; 49 else if(opthis == '*') res = data1 *data2; 50 else res = data2/data1; 51 data.push(res); 52 } 53 op.push(opc); 54 break; 55 case '*': 56 case '/': 57 if(!op.isEmpty()&&(op.peek()=='*'||op.peek()=='/')){ 58 char opthis = op.pop(); 59 int data1 = data.pop(); 60 int data2 = data.pop(); 61 int res = 0; 62 if(opthis == '*') res = data1 *data2; 63 else res = data2/data1; 64 data.push(res); 65 } 66 op.push(opc); 67 break; 68 } 69 } 70 } 71 while(!op.isEmpty()){ 72 char opthis = op.pop(); 73 int data1 = data.pop(); 74 int data2 = data.pop(); 75 int res = 0; 76 if(opthis=='+') res = data1+data2; 77 else if(opthis == '-') res = data2 -data1; 78 else if(opthis == '*') res = data1 *data2; 79 else res = data2/data1; 80 data.push(res); 81 } 82 return data.pop(); 83 } 84 }