leecode,rt_phm更新更新

This commit is contained in:
markilue 2023-05-22 13:52:33 +08:00
parent 3098bc28b3
commit 30aedfc4ab
10 changed files with 393 additions and 4 deletions

View File

@ -73,7 +73,6 @@ public class LC_1094_CarPooling {
} }
//二刷 //二刷
public boolean carPooling1(int[][] trips, int capacity) { public boolean carPooling1(int[][] trips, int capacity) {
//本质上就是判断某一个位置上是否超过capacity //本质上就是判断某一个位置上是否超过capacity
@ -138,4 +137,32 @@ public class LC_1094_CarPooling {
return true; return true;
} }
public boolean carPooling3(int[][] trips, int capacity) {
//构建差分数组
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] diff = new int[1001];
for (int[] trip : trips) {
if (trip[1] < min) min = trip[1];
if (trip[2] > max) max = trip[2];
diff[trip[1]] += trip[0];
diff[trip[2]] -= trip[0];
}
if (diff[min] > capacity) {
return false;
}
for (int i = min; i < max; i++) {
diff[i] += diff[i - 1];
if (diff[i] > capacity) {
return false;
}
}
return true;
}
} }

View File

@ -153,4 +153,22 @@ public class LC_1109_CorpFlightBookings {
return diff; return diff;
} }
//四刷:
public int[] corpFlightBookings5(int[][] bookings, int n) {
int[] diff = new int[n];
for (int[] booking : bookings) {
diff[booking[0] - 1] += booking[2];
if (booking[1] < n) {
diff[booking[1]] -= booking[2];
}
}
//通过差分数组还原当前位置
for (int i = 1; i < diff.length; i++) {
diff[i] += diff[i - 1];
}
return diff;
}
} }

View File

@ -0,0 +1,32 @@
package com.markilue.leecode.hot100.interviewHot.graph;
import java.util.ArrayList;
import java.util.List;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.graph
*@Author: markilue
*@CreateTime: 2023-05-22 12:02
*@Description: TODO 力扣207 课程表:
*@Version: 1.0
*/
public class LC_207_CanFinish {
List<List<Integer>> edge;//
//本质上就是图的深度优先搜索:检测这个图是否存在环路
public boolean canFinish(int numCourses, int[][] prerequisites) {
//使用一个list记录每个节点的子节点使用dfs判断是否会回到父节点
for (int[] prerequisite : prerequisites) {
List<Integer> list = edge.get(prerequisite[0]);
if (list == null) list = new ArrayList<>();
else list.add(prerequisite[1]);//添加子节点
}
return false;
}
}

View File

@ -12,6 +12,7 @@ import com.markilue.leecode.listnode.ListNode;
*/ */
public class LC_82_DeleteDuplicatesII { public class LC_82_DeleteDuplicatesII {
public ListNode deleteDuplicates(ListNode head) { public ListNode deleteDuplicates(ListNode head) {
ListNode fake = new ListNode(-101); ListNode fake = new ListNode(-101);
fake.next = head; fake.next = head;
@ -52,4 +53,29 @@ public class LC_82_DeleteDuplicatesII {
return fake.next; return fake.next;
} }
public ListNode deleteDuplicates2(ListNode head) {
ListNode fake = new ListNode();
fake.next = head;
ListNode cur = fake;
while (cur != null && cur.next != null) {
if (cur.next.next != null && cur.next.val == cur.next.next.val) {
//存在重复元素
ListNode temp = cur.next;
while (temp.next != null && temp.val == temp.next.val) {
temp = temp.next;
}
cur.next = temp.next;
} else {
cur = cur.next;
}
}
return fake.next;
}
} }

View File

@ -75,4 +75,28 @@ public class T34_75_SortColors {
} }
} }
//三刷:本质就是遇见1就直接交换遇见0需要判断一下是否移动了1
//但是这理论上来说不是原地排序因为排前面的
public void sortColors2(int[] nums) {
int index0 = 0;
int index1 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
swap(nums, index0, i);
if (index0 < index1) swap(nums, index1, i);//把1交换在前面去了,需要交换回来但是这样1就不是原地排序了
index0++;
index1++;
} else if (nums[i] == 1) {
swap(nums, index1, i);
index1++;
}
}
}
} }

View File

@ -81,4 +81,22 @@ public class T49_124_MaxPathSum {
return Math.max(leftMax, rightMax) + root.val; return Math.max(leftMax, rightMax) + root.val;
} }
public int maxPathSum2(TreeNode root) {
findMax1(root);
return maxSum;
}
//返回要当前节点的最大值
public int findMax1(TreeNode root) {
if (root == null) {
return 0;
}
int left = Math.max(findMax1(root.left), 0);
int right = Math.max(findMax1(root.right), 0);
maxSum = Math.max(left + right + root.val, maxSum);
return Math.max(left, right) + root.val;
}
} }

View File

@ -47,4 +47,17 @@ public class T53_141_HasCycle {
return false; return false;
} }
public boolean hasCycle2(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) return true;
}
return false;
}
} }

View File

@ -1,8 +1,8 @@
package com.markilue.leecode.hot100.second; package com.markilue.leecode.hot100.second;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
*@BelongsProject: Leecode *@BelongsProject: Leecode
@ -116,3 +116,90 @@ public class T55_146_LRUCache {
System.out.println(lRUCache.get(4)); // 返回 4 System.out.println(lRUCache.get(4)); // 返回 4
} }
} }
class LRUCache {
int capacity;
Node head;
Node tail;
Map<Integer, Node> map;
public LRUCache(int capacity) {
this.capacity = capacity;
map = new HashMap<>();
head = new Node();
tail = new Node();
head.next = tail;
tail.pre = head;
}
public int get(int key) {
Node node = map.get(key);
if (node == null) {
return -1;
} else {
removeNode(node);
addToHead(node);
return node.value;
}
}
public void put(int key, int value) {
Node node = map.get(key);
if (node == null) {
if (map.size() == capacity) {
//需要删除末尾的
Node del = deleteTail();
map.remove(del.key);
}
Node add = new Node(key, value);
map.put(key, add);
addToHead(add);
} else {
//map中存在这个数据只做更新
node.value = value;
removeNode(node);
addToHead(node);
}
}
private Node deleteTail() {
Node del = tail.pre;
removeNode(tail.pre);
return del;
}
private void removeNode(Node node) {
node.pre.next = node.next;
node.next.pre = node.pre;
}
private void addToHead(Node node) {
node.next = head.next;
head.next.pre = node;
node.pre = head;
head.next = node;
}
class Node {
int key;
int value;
Node pre;
Node next;
public Node() {
}
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
}

View File

@ -0,0 +1,27 @@
package com.markilue.leecode.hot100.second;
import com.markilue.leecode.listnode.ListNode;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-05-22 11:48
*@Description: TODO 力扣206 反转链表
*@Version: 1.0
*/
public class T63_206_ReverseList {
public ListNode reverseList(ListNode head) {
ListNode fake =new ListNode();
ListNode cur=head;
ListNode curNext;
while(cur!=null){
curNext=cur.next;
cur.next=fake.next;
fake.next=cur;
cur=curNext;
}
return fake.next;
}
}

View File

@ -0,0 +1,117 @@
package com.markilue.leecode.hot100.second;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.hot100.second
*@Author: markilue
*@CreateTime: 2023-05-22 11:49
*@Description: TODO 力扣207 课程表
*@Version: 1.0
*/
public class T64_207_CanFinish {
@Test
public void test() {
int numCourses = 2;
int[][] prerequisites = {{1, 0}, {0, 1}};
System.out.println(canFinish(numCourses, prerequisites));
}
//其实本质上就是监测是否存在环
List<List<Integer>> edge;//
//本质上就是图的深度优先搜索:检测这个图是否存在环路
//TODO 超出时间限制了:因为这样有很多重复的计算,一个节点作为别人的子节点时和自己就作为根节点时都计算了一次
public boolean canFinish(int numCourses, int[][] prerequisites) {
edge = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
edge.add(new ArrayList<>());
}
//使用一个list记录每个节点的子节点使用dfs判断是否会回到父节点
for (int[] prerequisite : prerequisites) {
List<Integer> list = edge.get(prerequisite[0]);
list.add(prerequisite[1]);//添加子节点
}
boolean[] used = new boolean[numCourses];
for (int i = 0; i < edge.size(); i++) {
if(!used[i]){
if (!dfs(i, used)) {
return false;
}
}
}
return true;
}
public boolean dfs(int start, boolean[] used) {
if (start >= edge.size()) {
return true;
}
used[start] = true;//当前节点已经遍历过
List<Integer> children = edge.get(start);
for (int i = 0; i < children.size(); i++) {
Integer cur = children.get(i);
if (used[cur]) {
return false;
}
if (!dfs(cur, used)) {
return false;
}
used[cur] = false;
}
return true;
}
boolean valid = true;
public boolean canFinish1(int numCourses, int[][] prerequisites) {
edge = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
edge.add(new ArrayList<>());
}
//使用一个list记录每个节点的子节点使用dfs判断是否会回到父节点
for (int[] prerequisite : prerequisites) {
edge.get(prerequisite[0]).add(prerequisite[1]);//添加子节点
}
int[] visited = new int[numCourses];
for (int i = 0; i < edge.size()&&valid; i++) {
if (visited[i] == 0) {
//从来没有遍历过
dfs(i, visited);
}
}
return valid;
}
public void dfs(int start, int[] visited) {
visited[start] = 1;//设置为遍历过了
List<Integer> childern = edge.get(start);
for (Integer child : childern) {
if (visited[child] == 0) {
//没有遍历过继续判断他是否可以
dfs(child, visited);
//判断是否还需要遍历
if (!valid) {
return;
}
} else if (visited[child] == 1) {
valid = false;//有问题,监测到环路了
return;
}
}
visited[start] = 2;//全都检查完了,没有问题
}
}