当前位置: 首页 > news >正文

【CodeTop】每日练习 2025.7.29

Leetcode 227. 基本计算器 II

栈的常见应用,定义好运算优先级,遇到当前运算优先级高于符号栈顶符号的情况进行计算,并保存结果。

class Solution {Map<Character, Integer> map = new HashMap<>() {{put('+', 1);put('-', 1);put('*', 2);put('/', 2);}};public int calculate(String s) {s = s.replaceAll(" ", "");char[] chS = s.toCharArray();int n = chS.length;Deque<Integer> nums = new ArrayDeque<>();Deque<Character> ops = new ArrayDeque<>();for (int i = 0; i < n; i++) {char cur = chS[i];if (cur == '(') {ops.addLast(cur);} else if (cur == ')') {while (!ops.isEmpty()) {if (ops.peekLast() != '(') {cal(nums, ops);} else {ops.pollLast();break;}}} else {if (Character.isDigit(cur)) {int num = 0;int j = i;while (j < n && Character.isDigit(chS[j])) {num = num * 10 + chS[j++] - '0';}nums.addLast(num);i = j - 1;} else {if (i > 0 && (chS[i - 1] == '(' || chS[i - 1] == '+' || chS[i - 1] == '-')) {nums.addLast(0);}while (!ops.isEmpty() && ops.peekLast() != '(') {char pre = ops.peekLast();if (map.get(pre) >= map.get(cur)) {cal(nums, ops);} else {break;}}ops.addLast(cur);}}}while (!ops.isEmpty()) {cal(nums, ops);}return nums.peekLast();}private void cal(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();int res = 0;if (op == '+') {res = a + b;} else if (op == '-') {res = a - b;} else if (op == '*') {res = a * b;} else if (op == '/') {res = a / b;}nums.addLast(res);}
}

Leetcode 169. 多数元素

摩尔投票,针对众数出现超过元素总数一半次数的集合,依次枚举可能的结果,遇到当前枚举的元素增加分数,否则减少分数,当分数为零时重新枚举下一个元素,最后一个被枚举的元素就是众数。

class Solution {public int majorityElement(int[] nums) {int res = 0, count = 0;for (int num : nums) {if (count == 0) {res = num;}count += num == res ? 1 : -1;}return res;}
}

Leetcode 226. 翻转二叉树

递归地在每个节点处交换左右子树,最终返回原根节点即可。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) {return root;}TreeNode left = invertTree(root.left);TreeNode right = invertTree(root.right);root.left = right;root.right = left;return root;}
}

Leetcode 209. 长度最小的子数组

滑窗模板题,理解的基础上记住。

class Solution {public int minSubArrayLen(int target, int[] nums) {int res = Integer.MAX_VALUE;int sum = 0;for (int left = 0, right = 0; right < nums.length; right++) {sum += nums[right];while (sum >= target) {res = Math.min(res, right - left + 1);sum -= nums[left++];}}return res != Integer.MAX_VALUE ? res : 0;}
}

Leetcode 718. 最长重复子数组

类似于最长公共子序列,动态规划解决。

class Solution {public int findLength(int[] nums1, int[] nums2) {int m = nums1.length;int n = nums2.length;int res = 0;int[][] dp = new int[m + 1][n + 1];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (nums1[i] == nums2[j]) {dp[i + 1][j + 1] = dp[i][j] + 1;res = Math.max(res, dp[i + 1][j + 1]);}}}return res;}
}
http://www.lryc.cn/news/603361.html

相关文章:

  • LibTorch使用-基础版
  • Jetpack - Room(Room 引入、Room 优化)
  • Spring Boot 自动配置:从 2.x 到 3.x 的进化之路
  • 牛顿拉夫逊法PQ分解法计算潮流MATLAB程序计算模型。
  • 微信小程序私密消息
  • GaussDB 数据库架构师修炼(十) 性能诊断常用视图
  • 原生html+js+jq+less 实现时间区间下拉弹窗选择器
  • 鸿蒙网络编程系列59-仓颉版TLS回声服务器示例
  • 42、鸿蒙HarmonyOS Next开发:应用上下文Context
  • Apache Ignite 的分布式原子类型(Atomic Types)
  • 专业Python爬虫实战教程:逆向加密接口与验证码突破完整案例
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 微博文章数据可视化分析-文章评论量分析实现
  • Apache Ignite Cluster Groups的介绍
  • U3D中的package
  • 【PHP】Swoole:CentOS安装Composer+Hyperf
  • vue2 使用liveplayer加载视频
  • .NET Core 3.1 升级到 .NET 8
  • 自学嵌入式 day37 HTML
  • 前端代码格式化工具HTML离线版
  • LangChain学习笔记01---基本概念及使用
  • SkSurface---像素的容器:表面
  • echarts饼图
  • .NET测试平台Parasoft dotTEST在汽车电子行业的核心功能及应用
  • OpenAI Python API 完全指南:从入门到实战
  • 使用jQuery动态操作HTML和CSS
  • 从centos更换至ubuntu的安装、配置、操作记录
  • 系统选择菜单(ubuntu grub)介绍
  • 智能健康项链专利拆解:ECG 与 TBI 双模态监测的硬件架构与信号融合
  • Ubuntu22.04系统安装,Nvidia显卡驱动安装问题
  • 【Linux系统编程】Ext2文件系统