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);
    }
}
相关推荐
Serene_Dream7 小时前
JVM 并发 GC - 三色标记
jvm·面试
HY小宝F10 小时前
职场沟通的深层智慧:从对抗到协作的自我修炼
职场和发展
愚者游世10 小时前
Delegating Constructor(委托构造函数)各版本异同
开发语言·c++·程序人生·面试·改行学it
AI职业加油站11 小时前
职业提升之路:我的大数据分析师学习与备考分享
大数据·人工智能·经验分享·学习·职场和发展·数据分析
信码由缰11 小时前
Spring Boot 面试问题
spring boot·后端·面试
草履虫建模18 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
马猴烧酒.1 天前
【面试八股|Java集合】Java集合常考面试题详解
java·开发语言·python·面试·八股
学历真的很重要1 天前
【系统架构师】第二章 操作系统知识 - 第二部分:进程管理(详解版)
学习·职场和发展·系统架构·系统架构师
闻哥1 天前
从测试坏味道到优雅实践:打造高质量单元测试
java·面试·单元测试·log4j·springboot
VT.馒头1 天前
【力扣】2722. 根据 ID 合并两个数组
javascript·算法·leetcode·职场和发展·typescript