Loading...

表达式求值

在这里插入图片描述

求解代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
HashMap<Character,Integer> map = new HashMap<Character,Integer>(){{
        put('+',1);
        put('-',1);
        put('*',2);
    }};

    void calc(LinkedList<Integer> nums,LinkedList<Character> operators){
        if(nums.isEmpty()||nums.size()<2){
            return;
        }

        if(operators.isEmpty()){
            return;
        }

        int b = nums.pollLast();
        int a = nums.pollLast();
        char operator = operators.pollLast();

        int res = 0;

        if(operator=='+'){
            res = a+b;
        }else if(operator=='-'){
            res = a-b;
        }else if(operator=='*'){
            res = a*b;
        }
        nums.addLast(res);
    }
    public int solve(String s) {
        s = s.replaceAll(" ", "");

        char[] str = s.toCharArray();
        int n = s.length();


        LinkedList<Integer> nums = new LinkedList<>();
        LinkedList<Character> operators = new LinkedList<>();
        nums.addLast(0);

        for(int i=0;i<n;i++){
            char c = str[i];

            if(c=='('){
                operators.addLast(c);
            }else if(c==')'){
                while(!operators.isEmpty()){
                    if(operators.peekLast()!='('){
                        calc(nums,operators);
                    }else{
                        operators.pollLast();
                        break;
                    }
                }
            }else{
                if(Character.isDigit(c)){
                    int base = 0;
                    int j=i;
                    while(j<n&&Character.isDigit(str[j])){
                        base=base*10+(str[j++]-'0');
                    }
                    nums.addLast(base);
                    i=j-1;
                }else{
                    if(i>0&&(str[i-1]=='('||str[i-1]=='+'||str[i-1]=='-')){
                        nums.addLast(0);
                    }

                    while(!operators.isEmpty()&&operators.peekLast()!='('){
                        char prev = operators.peekLast();
                        if(map.get(prev)>=map.get(c)){
                            calc(nums,operators);
                        }else{
                            break;
                        }
                    }
                    operators.addLast(c);
                }
            }
        }

        while(!operators.isEmpty()&&operators.peekLast()!='('){
            calc(nums,operators);
        }
        return nums.peekLast();
        

    }

小贴士

1.nums.addLast(0) 主要用于处理【表达式以正负号开头】的场景,比如 -1+2+3*4,可以把 -1 转为 0-1,把 +3 转为 0+3

2.i = j - 1可以修正外层 for 循环的索引i,让i直接跳到「当前多位数的最后一位」,抵消 for 循环的自动i++,避免重复遍历已经处理过的数字字符。、

比如表达式123+4,j=3

执行 i = j - 1 → i = 3 - 1 = 2,然后,外层 for 循环执行自动 i++ → i2变成3,下一轮循环,i=3,正好读取字符str[3] → '+'

最后更新于 2026-04-05 17:35:33
Code Road Record