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

【CodeTop】每日练习 2025.7.8

Leetcode 31. 下一个排列

从右向左找到第一个后续有更大值的元素,然后交换这个元素与更大值,从这个交换的位置直到末尾进行排序。

class Solution {public void nextPermutation(int[] nums) {int n = nums.length;int i = n - 2;while (i >= 0 && nums[i] >= nums[i + 1]) {i--;}if (i >= 0) {int j = n - 1;while (nums[j] <= nums[i]) {j--;}swap(nums, i, j);}reverse(nums, i + 1, n - 1);}private void swap (int[] nums, int i, int j) {int temp = nums[i];nums[i] = nums[j];nums[j] = temp;}private void reverse(int[] nums, int left, int right) {while (left < right) {swap(nums, left++, right--);}}
}

Leetcode 239. 滑动窗口最大值

单调队列模板题,比当前元素小的从尾部出队,超出范围的从队头出队。

class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int n = nums.length;int[] res = new int[n - k + 1];int[] queue = new int[n];int head = 0, tail = -1;for (int i = 0; i < n; i++) {while (head <= tail && nums[queue[tail]] <= nums[i]) {tail--;}queue[++tail] = i;if (queue[head] <= i - k) {head++;}if (i >= k - 1) {res[i - k + 1] = nums[queue[head]];}}return res;}
}

Leetcode 69. x 的平方根

二分答案法解决,开区间的写法容易确定返回值的情况。

class Solution {public int mySqrt(int x) {int left = 0;int right = Math.min(x, 46340) + 1;while (left + 1 < right) {int mid = left + ((right - left) >>> 1);if (mid * mid <= x) {left = mid;} else {right = mid;}}return left;}
}

Leetcode 32. 最长有效括号

括号匹配一般考虑用栈来解决,但这题用栈的逻辑反而有些反直觉,动态规划的做法更基础。
使用贪心算法则只需要统计左右括号的数量即可,容易记住。

class Solution {public int longestValidParentheses(String s) {int left = 0, right = 0, res = 0;for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == '(') {left++;} else {right++;}if (left == right) {res = Math.max(res, 2 * right);} else if (right > left) {left = right = 0;}}left = right = 0;for (int i = s.length() - 1; i >= 0; i--) {if (s.charAt(i) == '(') {left++;} else {right++;}if (left == right) {res = Math.max(res, 2 * left);} else if (left > right) {left = right = 0;}}return res;}
}

Leetcode 2. 两数相加

常规链表题,依次计算每位上的数值,注意处理进位和多余的数就可以。

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode dummy = new ListNode();ListNode cur1 = l1, cur2 = l2, cur = dummy;int carry = 0, sum = 0;while (cur1 != null && cur2 != null) {sum = cur1.val + cur2.val + carry;cur.next = new ListNode(sum % 10);carry = sum / 10;cur1 = cur1.next;cur2 = cur2.next;cur = cur.next;}while (cur1 != null) {sum = cur1.val + carry;cur.next = new ListNode(sum % 10);carry = sum / 10;cur1 = cur1.next;cur = cur.next;}while (cur2 != null) {sum = cur2.val + carry;cur.next = new ListNode(sum % 10);carry = sum / 10;cur2 = cur2.next;cur = cur.next;}if (carry != 0) {cur.next = new ListNode(1);}return dummy.next;}
}
http://www.dtcms.com/a/270135.html

相关文章:

  • Java 阻塞队列:7种类型全解析
  • 起重机械的工作循环门限值计算逻辑
  • 容器技术入门与Docker环境部署
  • Ntfs!LfsRestartLogFile函数分析之两次调用Ntfs!LfsReadRestart函数的目的
  • (生活比喻-图文并茂)http2.0和http3.0的队头阻塞,http2.0应用层解决,TCP层存在,3.0就是彻底解决,到底怎么理解区别???
  • AI健康小屋“15分钟服务圈”:如何重构社区健康生态?
  • MyBatis-Plus:深入探索与最佳实践
  • C#,js如何对网页超文本内容按行拆分,选择第A-B个字符返回HTM?
  • stack_queue扩展学习 --- 反向迭代器
  • 戴尔3670装win11和ubuntu双系统踩坑教程
  • 自动驾驶传感器的标定与数据融合
  • 【Android】组件及布局介绍
  • CAN主站转Modbus TCP网关:高铁门控系统的“毫秒级响应”密码
  • 【ZYNQ Linux开发】BRAM的几种驱动方式
  • 微服务集成snail-job分布式定时任务系统实践
  • Mac安装Docker(使用orbstack代替)
  • 单机分布式一体化数据库的架构设计与优化
  • 一个猜想不等式的推广
  • 业务分析技术实践篇
  • kafka集群安装
  • 让事情变得更好
  • Shader面试题100道之(21-40)
  • 光流 | RAFT光流算法如何改进提升
  • 【适合 Java 工程师的 AI 转型方向】
  • 基于PHP/MySQL的企业培训考试系统源码,高并发、稳定运行,源码开源可二开
  • Java中的生产消费模型解析
  • Distance Information Improves Heterogeneous Graph Neural Networks
  • 质量小议56 - 说教
  • [ESP32]VSCODE+ESP-IDF环境搭建及blink例程尝试(win10 win11均配置成功)
  • vscode打开stm32CubeIDE的项目的注释问题