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;
}
}
快慢指针:
flag运用的原理:
/**
* 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 ,请将其按 升序 排列并返回 排序后的链表 。
Sort List (归并排序链表)⭐
归并排序(递归法,快慢指针):
/**
* 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 sortList(ListNode head) {
//当链表为空或链表只有一个元素时直接返回。
if(head == null || head.next == null){
return head;
}
//定义快慢指针来找出链表中的中间节点
ListNode fast = head;
ListNode slow = head;
//通过slow来找到中间节点
while(fast.next != null && fast.next.next !=null){
//快指针移动两步,慢指针移动一步
fast = fast.next.next;
slow = slow.next;
}
ListNode temp = slow.next;
//找到中点 slow 后,执行 slow.next = None 将链表切断。
slow.next = null;
//递归分割时,输入当前链表左端点 head 和中心节点 slow 的下一个节点 tmp(因为链表是从 slow 切断的)。
ListNode left = sortList(head);
ListNode right = sortList(temp);
//定义一个返回结果的伪节点的指针,初始化一个新的空节点,值为0,指向该节点的指针为dumpy
ListNode dumpy = new ListNode(0);
//定义一个用于遍历链表的伪节点
ListNode prev =dumpy;
//当左链表和右链表不为空时,依次比较两个链表节点的值(归并排序核心)
while(left !=null && right !=null){
if(left.val <= right.val){
prev.next = left;
left = left.next;
}else{
prev.next = right;
right = right.next;
}
prev = prev.next;
}
//如果左链表有剩余元素,就拼接左链表,否则就拼接右链表
prev.next = left != null ? left : right;
//返回伪节点的指向的头节点
return dumpy.next;
}
}
归并排序(从底至顶直接合并):
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if(head == null){
return head;
}
// 1. 首先从头向后遍历,统计链表长度
int length = 0;
ListNode node = head;
while(node != null){
length++;
node = node.next;
}
// 2. 初始化,引入dummynode
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
// 3. 每次将链表拆分成若干个长度为subLen的子链表 , 并按照每两个子链表一组进行合并
for(int subLen = 1;subLen < length;subLen <<= 1){ // subLen每次左移一位(即sublen = sublen*2) PS:位运算对CPU来说效率更高
ListNode prev = dummyHead;
ListNode curr = dummyHead.next; // curr用于记录拆分链表的位置
while(curr != null){ // 如果链表没有被拆完
// 3.1 拆分subLen长度的链表1
ListNode head_1 = curr; // 第一个链表的头,即 curr初始的位置
for(int i = 1; i < subLen && curr != null && curr.next != null; i++){ // 拆分出长度为subLen的链表1
curr = curr.next;
}
// 3.2 拆分subLen长度的链表2
ListNode head_2 = curr.next; // 第二个链表的头 即 链表1尾部的下一个位置
curr.next = null; // 断开第一个链表和第二个链表的链接
curr = head_2; // 第二个链表头 重新赋值给curr
for(int i = 1;i < subLen && curr != null && curr.next != null;i++){ // 再拆分出长度为subLen的链表2
curr = curr.next;
}
// 3.3 再次断开 第二个链表最后的next的链接
ListNode next = null;
if(curr != null){
next = curr.next; // next用于记录 拆分完两个链表的结束位置
curr.next = null; // 断开连接
}
// 3.4 合并两个subLen长度的有序链表
ListNode merged = mergeTwoLists(head_1,head_2);
prev.next = merged; // prev.next 指向排好序链表的头
while(prev.next != null){ // while循环 将prev移动到 subLen*2 的位置后去
prev = prev.next;
}
curr = next; // next用于记录 拆分完两个链表的结束位置
}
}
// 返回新排好序的链表
return dummyHead.next;
}
public ListNode mergeTwoLists(ListNode left, ListNode right) {
ListNode dumpy = new ListNode(0);
ListNode prev =dumpy;
while(left !=null && right !=null){
if(left.val <= right.val){
prev.next = left;
left = left.next;
}else{
prev.next = right;
right = right.next;
}
prev = prev.next;
}
prev.next = left != null ? left : right;
return dumpy.next;
}
}
152.给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
乘积最大子序列画解⭐
class Solution {
public int maxProduct(int[] nums) {
int max = Integer.MIN_VALUE, imax = 1, imin = 1;
for(int i=0; i<nums.length; i++){
//当负数出现时则imax与imin进行交换再进行下一步计算
if(nums[i] < 0){
int tmp = imax;
imax = imin;
imin = tmp;
}
imax = Math.max(imax*nums[i], nums[i]);
//由于负数会导致最大的变最小的,最小的变最大的,因此还需要维护当前最小值imin
imin = Math.min(imin*nums[i], nums[i]);
max = Math.max(max, imax);
}
return max;
}
}