6.20 二刷hot100 矩阵+链表开头

This commit is contained in:
zhangsan 2025-06-20 13:28:35 +08:00
parent af0416096f
commit c1899dd270
4 changed files with 44 additions and 4 deletions

View File

@ -5,14 +5,16 @@ import java.util.HashSet;
/** /**
* 题目160. 相交链表 (getIntersectionNode) * 题目160. 相交链表 (getIntersectionNode)
* 描述给你两个单链表的头节点 headA headB 请你找出并返回两个单链表相交的起始节点如果两个链表不存在相交节点返回 null * 描述给你两个单链表的头节点 headA headB 请你找出并返回两个单链表相交的起始节点如果两个链表不存在相交节点返回 null
*
进阶你能否设计一个时间复杂度 O(m + n) 仅用 O(1) 内存的解决方案
* 链接https://leetcode.cn/problems/intersection-of-two-linked-lists/ * 链接https://leetcode.cn/problems/intersection-of-two-linked-lists/
*/ */
//没想出高效方法 //没想出高效方法
//二刷会做
public class GetIntersectionNode { public class GetIntersectionNode {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
HashSet<ListNode> visited = new HashSet<ListNode>(); HashSet<ListNode> visited = new HashSet<ListNode>();
ListNode temp = headA; ListNode temp = headA;
while (temp != null) { while (temp != null) {
@ -28,8 +30,42 @@ public class GetIntersectionNode {
} }
return null; return null;
} }
//高效方法 //二刷想到的时间复杂度O(m + n)但是有点啰嗦
public ListNode getIntersectionNode1(ListNode headA, ListNode headB) { 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 + ca 是独有部分c 是公共部分 链表 B 的长度为 n = b + c 要么相遇在交点要么都走到 null
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA, B = headB; ListNode A = headA, B = headB;
while (A != B) { while (A != B) {
A = A != null ? A.next : headB; A = A != null ? A.next : headB;

View File

@ -6,6 +6,7 @@ package linkedlist;
* 链接https://leetcode.cn/problems/reverse-linked-list/ * 链接https://leetcode.cn/problems/reverse-linked-list/
*/ */
//二刷会做
public class ReverseList { public class ReverseList {
public ListNode reverseList(ListNode head) { public ListNode reverseList(ListNode head) {
ListNode prev = null; ListNode prev = null;

View File

@ -15,7 +15,9 @@ package matrix;
输出[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] 输出[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
*/ */
//不会原地旋转只会借助辅助数组 //不会原地旋转只会借助辅助数组
//二刷不会
public class RotateImage { public class RotateImage {
//[i][j] [j][n - 1 - i]
public void rotate(int[][] matrix) { public void rotate(int[][] matrix) {
int n = matrix.length; int n = matrix.length;
int[][] matrix_new = new int[n][n]; int[][] matrix_new = new int[n][n];

View File

@ -16,6 +16,7 @@ package matrix;
输出false 输出false
*/ */
//高效的方法不会 //高效的方法不会
//二刷会做
public class SearchMatrix { public class SearchMatrix {
//矩阵逆时针旋转45右上角的元素看作树根左边比他小右边比他大 //矩阵逆时针旋转45右上角的元素看作树根左边比他小右边比他大
public boolean searchMatrix(int[][] matrix, int target) { public boolean searchMatrix(int[][] matrix, int target) {