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

【CodeTop】每日练习 2025.7.1

Leetcode 92. 反转链表 II

在基础的反转链表基础上限制了范围,只需要额外处理反转段前后的指针即可。

/*** 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 reverseBetween(ListNode head, int left, int right) {ListNode dummy = new ListNode(0, head);ListNode group = dummy;for (int i = 0; i < left - 1; i++) {group = group.next;}ListNode pre = null, cur = group.next, next;ListNode temp = head.next;for (int i = 0; i < right - left + 1; i++) {next = cur.next;cur.next = pre;pre = cur;cur = next;}group.next.next = cur;group.next = pre;return dummy.next;}
}

Leetcode 141. 环形链表

经典列表找环问题,定义快慢指针,快指针每轮走两步,慢指针每轮走一步,二者最终会在入环节点处相遇。

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {ListNode slow = head, fast = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if (slow == fast) {return true;}}return false;}
}

Leetcode 54. 螺旋矩阵

根据题意按方向遍历,遇到越界或是已访问过的情况,改变访问节点的方向。

class Solution {private static final int[][] DIRECTIONS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};public List<Integer> spiralOrder(int[][] matrix) {int m = matrix.length;int n = matrix[0].length;List<Integer> res = new ArrayList<>();for (int i = 0, j = 0, k = 0, direction = 0; k < m * n; k++) {res.add(matrix[i][j]);matrix[i][j] = Integer.MAX_VALUE;int x = i + DIRECTIONS[direction][0];int y = j + DIRECTIONS[direction][1];if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] == Integer.MAX_VALUE) {direction = (direction + 1) % 4;}i += DIRECTIONS[direction][0];j += DIRECTIONS[direction][1];}return res;}
}

Leetcode 300. 最长递增子序列

可以定义 d f s ( i ) dfs(i) dfs(i) 为以 i i i 位置元素为末尾的最长子序列长度,依靠根据记忆化搜索的实现转化成动态规划。
但是这样做时间复杂度会达到 O ( N 2 ) O(N ^ 2) O(N2),其中 N N N 表示数组长度。
如果将状态定义为长度为以 i i i 位置为末尾的子序列中的最小元素值,可以结合二分查找将效率优化到 O ( N l o g N ) O(NlogN) O(NlogN)

class Solution {public int lengthOfLIS(int[] nums) {List<Integer> dp = new ArrayList<>();for (int num : nums) {int index = binarySearch(dp, num);if (index == dp.size()) {dp.add(num);} else {dp.set(index, num);}}System.out.println(dp);}private int binarySearch(List<Integer> dp, int target) {int left = 0, right = dp.size();while (left < right) {int mid = left + ((right - left) >>> 1);if (dp.get(mid) < target) {left = mid + 1;} else {right = mid;}}return left;}
}

Leetcode 23. 合并 K 个升序链表

定义一个堆,初始状态下堆中存储每个链表的头节点元素,依次取出堆中最小的元素,并同时加入链表的后续节点,直到堆中元素全部处理完毕。

/*** 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 mergeKLists(ListNode[] lists) {PriorityQueue<ListNode> heap = new PriorityQueue<>((o1, o2) -> o1.val - o2.val);for (ListNode head : lists) {if (head != null) {heap.offer(head);}}ListNode dummy = new ListNode();ListNode cur = dummy;while (! heap.isEmpty()) {ListNode node = heap.poll();if (node.next != null) {heap.offer(node.next);}cur.next = node;cur = cur.next;}return dummy.next;}
}
http://www.dtcms.com/a/264034.html

相关文章:

  • 使用 Conda 工具链创建 Poetry 本地虚拟环境全记录——基于《Python 多版本与开发环境治理架构设计》
  • 迅为高情性6TOPS算力的RK3576开发板NPU rknn-model-zoo例程演示
  • Windows VMWare Centos Docker部署Springboot + mybatis + MySql应用
  • Windows版minio下载安装使用教程
  • 最大子数组和-力扣
  • 微软开源GitHub Copilot Chat,AI编程领域迎新突破
  • ChatGPT、DeepSeek等大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模
  • 康养休闲旅游服务实训室建设方案:数字化赋能与全链条实训创新
  • 在vscode中进行git推送拉取的详细方法
  • InnoDB索引
  • 深入解析NumPy的核心函数np.array()
  • ip网络基础
  • k8s一键部署tongweb企业版7049m6(by why+lqw)
  • 计网学习笔记第1章 计算机网络体系结构(灰灰题库)
  • 智能通信领域论文投稿常见问题与解决方案——基于数百篇CA检索稿件的实证分析
  • 【算法】动态规划 矩阵 :62. 不同路径
  • Android屏幕共享+WebSocket实现传输截图
  • tree 命令集成到 Git Bash:可视化目录结构的指南
  • 成为一名大数据平台SRE需要具备哪些基础技能-附录
  • 为什么js是单线程?
  • SpringMVC--使用RESTFul实现用户管理系统
  • MySQL 8.4 备份与恢复完全指南
  • 软件测试期末复习之白盒测试
  • 将svn项目迁移到git
  • 技术学习_人工智能_1_神经网络是如何实现的?
  • 【算法】动态规划 斐波那契类型: 740. 删除并获得点数
  • Vue 3.x 使用 “prerender-spa-plugin ” 预渲染实现网站 SEO 优化
  • 读Vista
  • AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月1日第125弹
  • 数据结构学习——图