Compare commits

..

No commits in common. "c1899dd27071ab3573c659c64a44fbae8e7925fa" and "46a9f57fb251b105739d94576f840d0bdd87ac4b" have entirely different histories.

6 changed files with 5 additions and 46 deletions

View File

@ -5,16 +5,14 @@ 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 getIntersectionNode1(ListNode headA, ListNode headB) { public ListNode getIntersectionNode(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) {
@ -30,42 +28,8 @@ public class GetIntersectionNode {
} }
return null; return null;
} }
//二刷想到的时间复杂度O(m + n)但是有点啰嗦 //高效方法
public ListNode getIntersectionNode2(ListNode headA, ListNode headB) { public ListNode getIntersectionNode1(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,7 +6,6 @@ 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

@ -1,5 +1,6 @@
package matrix; package matrix;
import java.util.List;
/** /**
* 题目59. 螺旋矩阵 II (generateMatrix) * 题目59. 螺旋矩阵 II (generateMatrix)
@ -12,7 +13,6 @@ package matrix;
* 链接https://leetcode.cn/problems/spiral-matrix-ii/ * 链接https://leetcode.cn/problems/spiral-matrix-ii/
*/ */
//二刷会做同54题一个思路
public class GenerateMatrix { public class GenerateMatrix {
public int[][] generateMatrix(int n) { public int[][] generateMatrix(int n) {
int[][]res=new int[n][n]; int[][]res=new int[n][n];

View File

@ -15,9 +15,7 @@ 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,7 +16,6 @@ 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) {

View File

@ -16,7 +16,6 @@ import java.util.List;
输出[1,2,3,4,8,12,11,10,9,5,6,7] 输出[1,2,3,4,8,12,11,10,9,5,6,7]
*/ */
//没想出 //没想出
//二刷会做
public class SpiralOrder { public class SpiralOrder {
public List<Integer> spiralOrder(int[][] matrix) { public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();