leetcode-day10

阅读 52

2022-02-09

139.给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。
注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
动态规划:

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        int n = s.length(), maxw = 0;
        boolean[] dp = new boolean[n + 1]; 
        dp[0] = true;
        Set<String> set = new HashSet();
        //将str放在HashSet中,更新maxw得到最长的字符串的长度
        for(String str : wordDict){
            set.add(str);
            maxw = Math.max(maxw, str.length());
        }
        //相当于一层层递进,从dp[0] = true开始,set若包含s的这一部分则更新dp[0+len]为true,最后看看dp[n]是否为true
        for(int i = 1; i <= n ; i++){
            for(int j = i; j >= 0 && j + maxw >= i; j--){
                if(dp[j] && set.contains(s.substring(j, i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
}

141.给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
环形链表
哈希表:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        //遍历所有节点,每次遍历到一个节点时,判断该节点此前是否被访问过。
        Set<ListNode> seen = new HashSet<ListNode>();
        while (head != null) {
            //Set集合是可以去重的,就是没有相同的元素。在执行add方法时候,如果这个元素已经在set中存在,那么就返回false,否则返回true。
            //有环的话就会提前返回true,没有的话就可以来到head=null
            if (!seen.add(head)) {
                return true;
            }
            head = head.next;
        }
        return false;
    }
}

快慢指针:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head; // 慢指针每次走1步
        ListNode fast = head; // 快指针每次走2步
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) return true;
        }
        return false;
    }
}

142.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。(类似于141)
不允许修改 链表。
哈希表:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        Set<ListNode> seen = new HashSet<ListNode>();
        while (head != null) {
            if (!seen.add(head))
            return head;
            head = head.next;
        }
        return null;
    }
}

快慢指针:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null)
            return null;
        ListNode fast = head;//快指针
        ListNode slow = head;//慢指针
        boolean flag = false;//判断有无环
        while(fast.next!=null && fast.next.next != null){ //无环 while也会退出不会死循环
            slow=slow.next;
            fast=fast.next.next;//快指针一次两步慢指针一次一步
            if(slow == fast) { //快指针与慢指针相遇则有环
                flag=true;
                break;
            }
        }
        if(flag){
            fast=head; //如果有环 fast移到头指针
            while(fast != slow){ //快慢指针一次一步 相遇则为入口 可以数学推导
                fast=fast.next;
                slow=slow.next;
            }
            return slow;
        }
        return null;
    }
}

146.请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。
148.给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
152.给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

精彩评论(0)

0 0 举报