Leetcode (力扣)做题记录 hot100(3,560,160,206)

力扣第三题:无重复字符的最长字串

3. 无重复字符的最长子串 - 力扣(LeetCode)

双指针,遍历字符串,我们主要需要关注找到map里面有的时候如何更新慢指针,注意+1和防止指针回退就好。

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap map = new HashMap<>();
        int slow = 0;
        int max = 0;
        for(int fast = 0; fast
力扣第560题:和为K的子数组

560. 和为 K 的子数组 - 力扣(LeetCode)

第一次代码如下:

双指针,慢指针调整位置,快指针做计算。

class Solution {
    public int subarraySum(int[] nums, int k) {

        int count = 0;
        for (int left = 0; left < nums.length; left++) {
            int sum =0;
            for (int right = left; right < nums.length ; right++) {
                sum = sum +nums[right];
                if(sum == k){
                    count++;
                }
            }
    
        }
    return count;
    }
}
力扣第160题:相交链表

160. 相交链表 - 力扣(LeetCode)

第一次代码如下:

当时想的是,兔子乌龟相遇的想法,后面发现是错误的,那是看是否有环,然后看了题解,写的确实nb!!感觉自已太笨了!!!循环A链表,到达null循环B B同理,两个链表要么一起到达同一个节点,要么同时到达null!!

/**
 * 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 a =headA;
        ListNode b = headB;
        while( a != b){
            a = a!=null?a.next :headB;
            b = b!=null?b.next:headA;
        }
        return b;
    }
}
力扣206题:反转链表

206. 反转链表 - 力扣(LeetCode)

/**
 * 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 ListNode reverseList(ListNode head) {
        ListNode temp = null;//存储下一个节点
        ListNode pre = null;//用于反转指向前面
        ListNode cur = head;
        while(cur != null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

 本文相关图片资源来自于网络中,如有侵权请联系删除!

你可能感兴趣的:(力扣日常,leetcode,散列表,算法)