概述
对Leetcode中的基本计算器的总结
只能计算加减的
public static int cal(String str){ Stack<Integer> stack=new Stack<>(); String sign="+"; int curnum=0; boolean hasNum=false; for(int i=0;i<str.length();i++) { char temp=str.charAt(i); if(Character.isDigit(temp)) { curnum=curnum*10+(temp-'0'); }else if(temp!=' ') { if(sign.equals("+")) { stack.push(curnum); }else if(sign.equals("-")) { stack.push(curnum*-1); } sign=String.valueOf(temp); curnum=0; } } if(sign.equals("+")) { stack.push(curnum); }else if(sign.equals("-")) { stack.push(curnum*-1); } return 0; }
加减乘除,不带括号
import java.util.Stack; class Solution { public int calculate(String str) { Stack<Integer> stack=new Stack<>(); int curnum=0; String sign="+"; for(int i=0;i<str.length();i++) { char temp=str.charAt(i); if(Character.isDigit(temp)) { curnum=curnum*10+temp-'0'; } if((!Character.isDigit(temp)&&temp!=' ')||i==str.length()-1) { if(sign.equals("+")) { stack.push(curnum); }else if(sign.equals("-")) { stack.push(curnum*-1); } else if(sign.equals("*")) { int t=stack.pop(); curnum=t*curnum; stack.push(curnum); } else if(sign.equals("/")) { int t=stack.pop(); curnum=t/curnum; stack.push(curnum); } curnum=0; sign=String.valueOf(temp); } } int res=0; for(Integer i:stack) { res+=i; } return res; } public static int calculate2(String s) { Stack<String> stack=new Stack<>(); char[] chars = s.toCharArray(); int curnum=0; boolean hasNum=false; for(int i=0;i<chars.length;i++) { if(chars[i]>='0'&&chars[i]<='9') { curnum=curnum*10+chars[i]-'0'; hasNum=true; }else if(chars[i]!=' ') { if(hasNum) { if(!stack.isEmpty()&&(stack.peek().equals("*")||stack.peek().equals("/"))) { String point=stack.pop(); int tempnum=Integer.parseInt(stack.pop()); if(point.equals("*")) { stack.push(String.valueOf(tempnum*curnum)); }else { stack.push(String.valueOf(tempnum/curnum)); } }else { stack.push(String.valueOf(curnum)); } curnum=0; hasNum=false; } stack.push(String.valueOf(chars[i])); } } if(hasNum) { if(!stack.isEmpty()&&(stack.peek().equals("*")||stack.peek().equals("/"))) { String point=stack.pop(); int tempnum=Integer.parseInt(stack.pop()); if(point.equals("*")) { stack.push(String.valueOf(tempnum*curnum)); }else { stack.push(String.valueOf(tempnum/curnum)); } }else { stack.push(String.valueOf(curnum)); } } int res=0; boolean flag=true; for(String str:stack) { if(str.equals("+")) { flag=true; }else if(str.equals("-")) { flag=false; }else { if(flag) res+=Integer.parseInt(str); else res-=Integer.parseInt(str); } } return res; } }
完整的代码
public static int calculate(String str) { Stack<Integer> stack=new Stack<>(); int[] res = cal(str, 0); return res[0]; } public static int[] cal(String str,int index) { Stack<Integer> stack=new Stack<>(); int curnum=0; String sign="+"; int i=index; for(;i<str.length();i++) { char temp=str.charAt(i); if(Character.isDigit(temp)) { curnum=curnum*10+temp-'0'; } if((!Character.isDigit(temp)&&temp!=' ')||i==str.length()-1) { if(temp=='(') { int[] arr=cal(str,i+1); curnum=arr[0]; if(curnum==-4) System.out.println(curnum); i=arr[1]+1; } if(sign.equals("+")) { stack.push(curnum); }else if(sign.equals("-")) { stack.push(curnum*-1); } else if(sign.equals("*")) { int t=stack.pop(); curnum=t*curnum; stack.push(curnum); } else if(sign.equals("/")) { int t=stack.pop(); curnum=t/curnum; stack.push(curnum); } if(i==str.length()-1||temp==')') break; curnum=0; sign=String.valueOf(temp); } } int res=0; for(Integer t:stack) { res+=t; } return new int[]{res,i}; }