力扣爆刷第87天之hot100五连刷21-25
文章目录
- 力扣爆刷第87天之hot100五连刷21-25
- 一、240. 搜索二维矩阵 II
- 二、160. 相交链表
- 三、206. 反转链表
- 四、234. 回文链表
- 五、141. 环形链表
一、240. 搜索二维矩阵 II
题目链接:https://leetcode.cn/problems/search-a-2d-matrix-ii/description/?envType=study-plan-v2&envId=top-100-liked
思路:从横向递增纵向递增的矩阵中搜索target,利用顺序的特性,从矩阵的右上角进行搜索,如果相等就返回,如果大于向下进行搜索,如果小于向左进行搜索,存在的话自然可以找到,不存在的话会一直搜索到边界才会停止,也就是当前算法还有改进的空间,可以采取早停措施。
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
return dfs(matrix, 0, matrix[0].length-1, target);
}
boolean dfs(int[][] matrix, int i, int j, int target) {
while(i < matrix.length && j >= 0) {
if(matrix[i][j] == target) {
return true;
}
if(matrix[i][j] > target) {
j--;
}else {
i++;
}
}
return false;
}
}
二、160. 相交链表
题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-liked
思路:经典题目,直接求长度,然后对齐,然后同步比较,相同即返回。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pa = headA, pb = headB;
int lenA = 0, lenB = 0;
while(pa != null) {
lenA++;
pa = pa.next;
}
while(pb != null) {
lenB++;
pb = pb.next;
}
pa = headA;
pb = headB;
while(lenA > lenB) {
pa = pa.next;
lenA--;
}
while(lenB > lenA) {
pb = pb.next;
lenB--;
}
while(pa != null) {
if(pa == pb) {
return pa;
}
pa = pa.next;
pb = pb.next;
}
return null;
}
}
三、206. 反转链表
题目链接:https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
思路:两个指针,一个指向头结点,一个用于向后走,遍历过程中用临时变量记录临时节点,然后采用头插法即可。
class Solution {
public ListNode reverseList(ListNode head) {
ListNode root = new ListNode();
ListNode p1 = root, p2 = head;
while(p2 != null) {
ListNode t = p2;
p2 = p2.next;
t.next = p1.next;
p1.next = t;
}
return root.next;
}
}
四、234. 回文链表
题目链接:https://leetcode.cn/problems/palindrome-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
思路:直接快慢指针定位中间节点,然后翻转其中一般的链表,然后遍历比较即可。
/**
* 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 boolean isPalindrome(ListNode head) {
ListNode p1 = head, p2 = head;
while(p2 != null && p2.next != null) {
p1 = p1.next;
p2 = p2.next.next;
}
if(p2 != null) {
p1 = p1.next;
}
p2 = reverse(p1);
p1 = head;
while(p2 != null) {
if(p1.val != p2.val) {
return false;
}
p1 = p1.next;
p2 = p2.next;
}
return true;
}
ListNode reverse(ListNode head) {
ListNode root = new ListNode();
ListNode p1 = root, p2 = head;
while(p2 != null) {
ListNode t = p2;
p2 = p2.next;
t.next = p1.next;
p1.next = t;
}
return root.next;
}
}
五、141. 环形链表
题目链接:https://leetcode.cn/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-100-liked
思路:判断是否有环,直接快慢指针。
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;
}
}