139.单词拆分(动态规划)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/354bf1e7d847b96982ae122f6e1b8184.png)
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
int len = s.length();
Set<String> set = new HashSet<>();
int maxLen = -1;
for(String word : wordDict) {
if(maxLen < word.length()) maxLen = word.length();
set.add(word);
}
boolean[] dp = new boolean[len + 1];
dp[0] = true;
for(int i = 1; i < dp.length; i++) {
for(int j = Math.max(0, i - maxLen); j < i; j++) {
if(dp[j] && set.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[len];
}
}
142.环形链表II(双指针)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d2698c76a8ad91f34c82852728ca5039.png)
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null) return null;
ListNode fast = head;
ListNode slow = head;
while(true) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) break;
}
fast = head;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}