热榜题解有错

表达式求值

http://www.nowcoder.com/practice/9566499a2e1546c0a257e885dfdbf30d

热榜第一 无法解决 400+(100/5)+5这个用例, 而我的可以。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String s = scan.nextLine();
            s = s.replace("[", "(");
            s = s.replace("{", "(");
            s = s.replace("]", ")");
            s = s.replace("}", ")");
            Main test = new Main();
            int res = test.solution(s);
            System.out.println(res);
        }
    }
    public int solution(String s) {
        //括号用递归处理
        char ops = '+';
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ' ') continue;
            if (Character.isDigit(c)) {
                int num = 0;
                while (i < s.length() && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                i--;
                if (ops == '+') stack.push(num);
                else if (ops == '-') stack.push(-num);
                else if (ops == '*') stack.push(stack.pop() * num);
                else if (ops == '/') stack.push(stack.pop() / num);
            }
            else if (c == '+' || c == '-' || c == '*' || c == '/') {
                ops = c;
            }
            else if (c == '(') {
                int left = i;
                int right = i + 1;
                int count = 1;
                while (right < s.length() && count > 0) {
                    if (s.charAt(right) == '(') count++;
                    else if (s.charAt(right) == ')') count--;
                    right++;
                }
                i = right - 1;
                int num = solution(s.substring(left + 1, right - 1));
                if (ops == '+') stack.push(num);
                else if (ops == '-') stack.push(-num);
                else if (ops == '*') stack.push(stack.pop() * num);
                else if (ops == '/') stack.push(stack.pop() / num);
            }
        }
        int res = 0;
        while (!stack.isEmpty()) res += stack.pop();
        return res;
    }
}
全部评论
好想***听听这道题的讲解
1 回复 分享
发布于 2023-03-27 10:18 甘肃
比榜首的工整
点赞 回复 分享
发布于 2023-02-06 15:59 广东
写的挺工整的。但是有冗余部分,可以合并一下 import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { String s = scan.nextLine(); s = s.replace("[", "("); s = s.replace("{", "("); s = s.replace("]", ")"); s = s.replace("}", ")"); int res = solution(s); System.out.println(res); } } public static int solution(String s) { char ops = '+'; Stack<integer> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') continue; if (c == '+' || c == '-' || c == '*' || c == '/') { ops = c; continue; } int num = 0; if (Character.isDigit(c)) { while (i < s.length() && Character.isDigit(s.charAt(i))) { num = num * 10 + s.charAt(i) - '0'; i++; } i--; } else if (c == '(') { int left = i; int right = i + 1; int count = 1; while (right < s.length() && count > 0) { if (s.charAt(right) == '(') count++; else if (s.charAt(right) == ')') count--; right++; } i = right - 1; num = solution(s.substring(left + 1, right - 1)); } if (ops == '+') stack.push(num); else if (ops == '-') stack.push(-num); else if (ops == '*') stack.push(stack.pop() * num); else if (ops == '/') stack.push(stack.pop() / num); } int res = 0; while (!stack.isEmpty()) res += stack.pop(); return res; } }</integer>
点赞 回复 分享
发布于 2023-04-27 02:56 美国
(400+(100/5))/5 这个用例结果就是错的
点赞 回复 分享
发布于 2023-08-09 15:29 江苏

相关推荐

oppo 应用软开 22*15+0.5*12
拿到了ssp完美:真的坎坷,但是你至少拿到这么多offer了!
点赞 评论 收藏
分享
评论
23
2
分享
牛客网
牛客企业服务