news 2026/11/6 7:27:54

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进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/5 5:27:57

vue基于Spring Boot的建筑材料管理系统的应用和研究_ug8y52z3

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

作者头像 李华
网站建设 2026/11/5 20:55:56

【大模型】-LangChain--RAG文档系统

文章目录1.完整代码2.结果展示3. RAG介绍1.完整代码 由于使用的是通义&#xff0c;所以代码改造了下&#xff0c;因为openAI需要钱 import streamlit as st import tempfile import osfrom langchain_classic.memory import ConversationBufferMemory from langchain_communi…

作者头像 李华
网站建设 2026/11/5 21:53:21

探索非线性电液伺服系统的模型自适应反步控制

非线性电液伺服系统模型自适应反步控制&#xff0c;考虑模型参数不确定性。pdf教程matlab/simulink源程序 s函数编写在自动化控制领域&#xff0c;非线性电液伺服系统一直是个热门研究对象。今天咱们就聊聊它基于模型自适应反步控制&#xff0c;并且考虑模型参数不确定性的实现…

作者头像 李华
网站建设 2026/11/6 0:29:13

10.8 总结

10.8 总结 作业回顾 1.1 索引练习节选 s hello 1 world 2 hello 3 Python # 获取s的长度 print(len(s)) # 30 # 获取第4个字符 print(s[3]) # l # 获取最后一个字符 print(s[-1]) # n # 获取第7个字符 print(s[6]) # 1 # 获取倒数第7个字符 print(s[-7]) # 空格【不显…

作者头像 李华