首页 > 其他 > 详细

数值表达式的计算-leetcode basic calculator

时间:2019-06-14 17:32:27      阅读:184      评论:0      收藏:0      [点我收藏+]
 1 public class Solution {
 2     
 3     int priority(char op){
 4         if(op == ‘+‘ || op == ‘-‘) return 1;
 5         else return 2;
 6     }
 7     boolean isNum(String s){
 8         return Character.isDigit(s.charAt(0));
 9     }
10     boolean isOp(char ch){
11         return ch == ‘+‘ || ch == ‘-‘ || ch == ‘*‘ || ch == ‘/‘;
12     }
13     long cal(long num1, char op, long num2){
14         if(op == ‘-‘) return num1 - num2;
15         else if(op == ‘+‘) return num1 + num2;
16         else if(op == ‘*‘) return num1 * num2;
17         else return num1 / num2;
18     }
19     void popOpAndTwoNums(Stack<Character> opStack, Stack<Long> numStack){
20         char op = opStack.pop();
21         long num2 = numStack.pop(), num1 = numStack.pop();
22         long num = cal(num1, op, num2);
23         numStack.push(num);
24     }
25     public ArrayList<String> split(String s){
26         ArrayList<String> array = new ArrayList<String>();
27         int i = 0;
28         while(i < s.length()){
29             if(s.charAt(i) == ‘(‘ || s.charAt(i) == ‘)‘ || isOp(s.charAt(i))){
30                 array.add(s.substring(i, i + 1));
31                 i++;
32             }else if(s.charAt(i) == ‘ ‘){
33                 i++;
34                 continue ;
35             }else{
36                 int j = i;
37                 while(j < s.length() && Character.isDigit(s.charAt(j))) j++;
38                 array.add(s.substring(i, j));
39                 i = j;
40             }
41         }
42         return array;
43     }
44     public int calculate(String s) {
45         ArrayList<String> array = split(s);
46         Stack<Character> opStack = new Stack<Character>();
47         Stack<Long> numStack = new Stack<Long>();
48         for(int i = 0; i < array.size(); i++){
49             if(isNum(array.get(i))){
50                 numStack.push(Long.parseLong(array.get(i)));
51             }else{
52                 char ch = array.get(i).charAt(0);
53                 if(ch == ‘(‘){
54                     opStack.push(ch);
55                 }else if(ch == ‘)‘){
56                     while(opStack.peek() != ‘(‘){
57                         popOpAndTwoNums(opStack, numStack);
58                     }
59                     opStack.pop();
60                 }else{
61                     if(opStack.size() > 0 && isOp(opStack.peek()) && priority(ch) <= priority(opStack.peek())){
62                         popOpAndTwoNums(opStack, numStack);
63                     }
64                     opStack.push(ch);
65                 }
66             }
67         }
68         while(numStack.size() > 1){
69             popOpAndTwoNums(opStack, numStack);
70         }
71         return numStack.peek().intValue();
72     }
73 }

 

数值表达式的计算-leetcode basic calculator

原文:https://www.cnblogs.com/liqiniuniu/p/11024229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!