From c1899dd27071ab3573c659c64a44fbae8e7925fa Mon Sep 17 00:00:00 2001 From: zhangsan <646228430@qq.com> Date: Fri, 20 Jun 2025 13:28:35 +0800 Subject: [PATCH] =?UTF-8?q?6.20=20=E4=BA=8C=E5=88=B7hot100=20=E7=9F=A9?= =?UTF-8?q?=E9=98=B5+=E9=93=BE=E8=A1=A8=E5=BC=80=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/linkedlist/GetIntersectionNode.java | 44 +++++++++++++++++-- src/main/java/linkedlist/ReverseList.java | 1 + src/main/java/matrix/RotateImage.java | 2 + src/main/java/matrix/SearchMatrix.java | 1 + 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/linkedlist/GetIntersectionNode.java b/src/main/java/linkedlist/GetIntersectionNode.java index 271d489..b00f969 100644 --- a/src/main/java/linkedlist/GetIntersectionNode.java +++ b/src/main/java/linkedlist/GetIntersectionNode.java @@ -5,14 +5,16 @@ import java.util.HashSet; /** * 题目:160. 相交链表 (getIntersectionNode) * 描述:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。 - + * + 进阶:你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案? * 链接:https://leetcode.cn/problems/intersection-of-two-linked-lists/ */ //没想出高效方法 +//二刷会做 public class GetIntersectionNode { - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + public ListNode getIntersectionNode1(ListNode headA, ListNode headB) { HashSet visited = new HashSet(); ListNode temp = headA; while (temp != null) { @@ -28,8 +30,42 @@ public class GetIntersectionNode { } return null; } - //高效方法 - public ListNode getIntersectionNode1(ListNode headA, ListNode headB) { + //二刷想到的,时间复杂度O(m + n),但是有点啰嗦 + public ListNode getIntersectionNode2(ListNode headA, ListNode headB) { + int lenA = 0, lenB = 0; + ListNode curA = headA, curB = headB; + + // 1. 统计长度 + while (curA != null) { + lenA++; + curA = curA.next; + } + while (curB != null) { + lenB++; + curB = curB.next; + } + + // 2. 让长的链表先走差值步 + curA = headA; + curB = headB; + if (lenA > lenB) { + for (int i = 0; i < lenA - lenB; i++) curA = curA.next; + } else { + for (int i = 0; i < lenB - lenA; i++) curB = curB.next; + } + + // 3. 同时前进,找相遇点 + while (curA != null && curB != null) { + if (curA == curB) return curA; // 找到相交点 + curA = curA.next; + curB = curB.next; + } + + return null; // 不相交 + } + + //高效方法,链表 A 的长度为 m = a + c(a 是独有部分,c 是公共部分) 链表 B 的长度为 n = b + c 要么相遇在交点,要么都走到 null。 + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode A = headA, B = headB; while (A != B) { A = A != null ? A.next : headB; diff --git a/src/main/java/linkedlist/ReverseList.java b/src/main/java/linkedlist/ReverseList.java index 9549180..cd92b67 100644 --- a/src/main/java/linkedlist/ReverseList.java +++ b/src/main/java/linkedlist/ReverseList.java @@ -6,6 +6,7 @@ package linkedlist; * 链接:https://leetcode.cn/problems/reverse-linked-list/ */ +//二刷会做 public class ReverseList { public ListNode reverseList(ListNode head) { ListNode prev = null; diff --git a/src/main/java/matrix/RotateImage.java b/src/main/java/matrix/RotateImage.java index 0e289a1..cfd93b2 100644 --- a/src/main/java/matrix/RotateImage.java +++ b/src/main/java/matrix/RotateImage.java @@ -15,7 +15,9 @@ package matrix; 输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] */ //不会原地旋转,只会借助辅助数组 +//二刷不会 public class RotateImage { + //[i][j] → [j][n - 1 - i] public void rotate(int[][] matrix) { int n = matrix.length; int[][] matrix_new = new int[n][n]; diff --git a/src/main/java/matrix/SearchMatrix.java b/src/main/java/matrix/SearchMatrix.java index fc2508f..3e1da42 100644 --- a/src/main/java/matrix/SearchMatrix.java +++ b/src/main/java/matrix/SearchMatrix.java @@ -16,6 +16,7 @@ package matrix; 输出:false */ //高效的方法不会 +//二刷会做 public class SearchMatrix { //矩阵逆时针旋转45,右上角的元素看作树根,左边比他小,右边比他大 public boolean searchMatrix(int[][] matrix, int target) {