力扣爆刷第87天之hot100五连刷21-25

力扣爆刷第87天之hot100五连刷21-25

一、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;
    }
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在VS Code上刷力扣LeetCode)题目,首先需要进行以下几步操作: 1. 安装VS Code插件:在VS Code中搜索并安装LeetCode插件。这个插件可以提供LeetCode题目的在线编写和提交功能,以及自动测试和调试代码的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【史上最强代码编辑器VS Code】之VS Code刷力扣LeetCode)题目](https://blog.csdn.net/weixin_44553006/article/details/105183522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [在 vscode 上刷力扣 Leetcode 可以这样来](https://blog.csdn.net/u012190388/article/details/121277555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [leetcode答案-algo-study:从零开始刷力扣(LeetCode),JavaScript语言,算法](https://download.csdn.net/download/weixin_38680764/19920528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年拼却醉颜红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值