news 2026/1/10 22:33:47

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
day33(12.14)——leetcode面试经典150

150. 逆波兰表达式求值

150. 逆波兰表达式求值

题目:

题解:

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. 基本计算器

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

题目:

题解:

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); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/14 21:00:54

vue基于Spring Boot的减肥健身养生人士饮食营养管理系统_5gn4225x

目录 具体实现截图项目介绍论文大纲核心代码部分展示项目运行指导结论源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作 具体实现截图 本系统&#xff08;程序源码数据库调试部署讲解&#xff09;同时还支持java、ThinkPHP、Node.js、Spring…

作者头像 李华
网站建设 2025/12/14 20:56:13

昇腾CANN从单算子到融合优化实战

目录 1 摘要 2 技术原理 2.1 架构设计理念解析 2.2 核心算法实现 2.2.1 三级流水线设计原理 2.2.2 Tiling策略与数据重用 2.3 性能特性分析 2.3.1 理论性能模型 2.3.2 实测性能数据 3 实战部分 3.1 完整可运行代码示例 3.2 分步骤实现指南 步骤1&#xff1a;环境配…

作者头像 李华
网站建设 2025/12/17 0:30:34

大数据项目阿里云抢占式服务器

一、学生有免费额度可以使用 查看是否有免费的额度&#xff1a; https://university.aliyun.com/?spm5176.29458888.J_9220772140.19.6e632868x2bj7D 或者&#xff1a; https://free.aliyun.com/?spm5176.28623341.J_9220772140.18.4c044519hKalBC 二、购买抢占式资源服务…

作者头像 李华
网站建设 2026/1/5 9:46:32

Flink源码阅读:如何生成JobGraph

前文我们介绍了 Flink 的四种执行图&#xff0c;并且通过源码了解了 Flink 的 StreamGraph 是怎么生成的&#xff0c;本文我们就一起来看下 Flink 的另一种执行图——JobGraph 是如何生成的。 StreamGraph 和 JobGraph 的区别 在正式开始之前&#xff0c;我们再来回顾一下 Stre…

作者头像 李华
网站建设 2025/12/14 20:50:50

21、GNU 开发实用工具:函数、变量与调试技巧

GNU 开发实用工具:函数、变量与调试技巧 1. 关联数组与命名栈 在开发过程中,关联数组和命名栈是非常实用的数据结构。对于关联数组,可使用 defined 函数来测试键是否存在。 defined Arguments: 1: Name of associative array2: The key to test Returns: $(true) if …

作者头像 李华
网站建设 2025/12/14 20:50:27

YOLOv8+PyQt5车辆类型检测(可以重新训练,yolov8模型,从图像、视频和摄像头三种路径识别检测,包含登陆页面、注册页面和检测页面)

资源包含可视化的车辆类型检测系统&#xff0c;基于最新的YOLOv8训练的车辆类型检测模型&#xff0c;和基于PyQt5制作的可视化车辆类型检测系统&#xff0c;包含登陆页面、注册页面和检测页面&#xff0c;该系统可自动检测和识别图片或视频当中出现的21种车辆类型&#xff0c;包…

作者头像 李华