day33(12.14)——leetcode面试经典150

150. 逆波兰表达式求值

150. 逆波兰表达式求值

题目:

题解:

java 复制代码
class Solution {
    public static boolean isNumeric(String str) {
        return str != null && str.matches("-?\\d+");
    }

    public int evalRPN(String[] tokens) {
        //将数字放在一个集合中
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<tokens.length;i++) {
            String c = tokens[i];
            if(isNumeric(c)) {
                list.add(Integer.valueOf(c));
            }
            else {
                Integer a = list.remove(list.size()-1);
                Integer b = list.remove(list.size()-1);
                Integer r = 0;
                if("+".equals(c)) {
                    r = a+b;
                }
                else if("-".equals(c)) {
                    r = b-a;
                }
                else if("*".equals(c)) {
                    r = b * a;
                }
                else {
                    r = (int)b/a;
                }
                list.add(r);
            }
        }
        return list.get(0);
    }
}

224. 基本计算器

224. 基本计算器

很难,很麻烦,要考虑的东西很多,就像我昨天考的六级一样

题目:

题解:

java 复制代码
class Solution {
    public int calculate(String s) {
        // 存放所有的数字
        Deque<Integer> nums = new ArrayDeque<>();
        // 为了防止第一个数为负数,先往 nums 加个 0
        nums.addLast(0);
        // 将所有的空格去掉
        s = s.replaceAll(" ", "");
        // 存放所有的操作,包括 +/-
        Deque<Character> ops = new ArrayDeque<>();
        int n = s.length();
        char[] cs = s.toCharArray();
        for (int i = 0; i < n; i++) {
            char c = cs[i];
            if (c == '(') {
                ops.addLast(c);
            } else if (c == ')') {
                // 计算到最近一个左括号为止
                while (!ops.isEmpty()) {
                    char op = ops.peekLast();
                    if (op != '(') {
                        calc(nums, ops);
                    } else {
                        ops.pollLast();
                        break;
                    }
                }
            } else {
                if (isNum(c)) {
                    int u = 0;
                    int j = i;
                    // 将从 i 位置开始后面的连续数字整体取出,加入 nums
                    while (j < n && isNum(cs[j])) u = u * 10 + (int)(cs[j++] - '0');
                    nums.addLast(u);
                    i = j - 1;
                } else {
                    if (i > 0 && (cs[i - 1] == '(' || cs[i - 1] == '+' || cs[i - 1] == '-')) {
                        nums.addLast(0);
                    }
                    // 有一个新操作要入栈时,先把栈内可以算的都算了
                    while (!ops.isEmpty() && ops.peekLast() != '(') calc(nums, ops);
                    ops.addLast(c);
                }
            }
        }
        while (!ops.isEmpty()) calc(nums, ops);
        return nums.peekLast();
    }
    void calc(Deque<Integer> nums, Deque<Character> ops) {
        if (nums.isEmpty() || nums.size() < 2) return;
        if (ops.isEmpty()) return;
        int b = nums.pollLast(), a = nums.pollLast();
        char op = ops.pollLast();
        nums.addLast(op == '+' ? a + b : a - b);
    }
    boolean isNum(char c) {
        return Character.isDigit(c);
    }
}
相关推荐
程序员祥云2 小时前
技能特⻓回答
前端·面试
程序员小寒3 小时前
前端高频面试题之Promise相关方法
前端·javascript·面试
CoderYanger11 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
初辰ge11 小时前
2025年12月高质量简历模板平台深度解析
面试
老马啸西风12 小时前
成熟企业级技术平台-10-跳板机 / 堡垒机(Bastion Host)详解
人工智能·深度学习·算法·职场和发展
摇滚侠13 小时前
面试实战 问题三十三 Spring 事务常用注解
数据库·spring·面试
老马啸西风13 小时前
成熟企业级技术平台-09-加密机 / 密钥管理服务 KMSS(Key Management & Security Service)
人工智能·深度学习·算法·职场和发展
while(1){yan}13 小时前
网络基础知识
java·网络·青少年编程·面试·电脑常识
CoderYanger15 小时前
C.滑动窗口-求子数组个数-越长越合法——3325. 字符至少出现 K 次的子字符串 I
c语言·数据结构·算法·leetcode·职场和发展·哈希算法·散列表