Compare commits
2 Commits
46a9f57fb2
...
c1899dd270
Author | SHA1 | Date | |
---|---|---|---|
c1899dd270 | |||
af0416096f |
@ -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 + c(a 是独有部分,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;
|
||||||
|
@ -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;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package matrix;
|
package matrix;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 题目:59. 螺旋矩阵 II (generateMatrix)
|
* 题目:59. 螺旋矩阵 II (generateMatrix)
|
||||||
@ -13,6 +12,7 @@ import java.util.List;
|
|||||||
|
|
||||||
* 链接: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];
|
||||||
|
@ -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];
|
||||||
|
@ -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) {
|
||||||
|
@ -16,6 +16,7 @@ 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<>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user