diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java index 9c01fcb..abfd923 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1094_CarPooling.java @@ -73,7 +73,6 @@ public class LC_1094_CarPooling { } - //二刷 public boolean carPooling1(int[][] trips, int capacity) { //本质上就是判断某一个位置上是否超过capacity @@ -138,4 +137,32 @@ public class LC_1094_CarPooling { 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; + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java index d6f6497..8d9c773 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/difference/LC_1109_CorpFlightBookings.java @@ -153,4 +153,22 @@ public class LC_1109_CorpFlightBookings { 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; + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/graph/LC_207_CanFinish.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/graph/LC_207_CanFinish.java new file mode 100644 index 0000000..c169400 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/graph/LC_207_CanFinish.java @@ -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> edge;//边 + + //本质上就是图的深度优先搜索:检测这个图是否存在环路 + public boolean canFinish(int numCourses, int[][] prerequisites) { + + //使用一个list记录每个节点的子节点,使用dfs判断是否会回到父节点 + for (int[] prerequisite : prerequisites) { + List list = edge.get(prerequisite[0]); + if (list == null) list = new ArrayList<>(); + else list.add(prerequisite[1]);//添加子节点 + } + + + return false; + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_82_DeleteDuplicatesII.java b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_82_DeleteDuplicatesII.java index fd580d1..88a4760 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_82_DeleteDuplicatesII.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/interviewHot/listnode/LC_82_DeleteDuplicatesII.java @@ -12,6 +12,7 @@ import com.markilue.leecode.listnode.ListNode; */ public class LC_82_DeleteDuplicatesII { + public ListNode deleteDuplicates(ListNode head) { ListNode fake = new ListNode(-101); fake.next = head; @@ -52,4 +53,29 @@ public class LC_82_DeleteDuplicatesII { 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; + + } + + } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java index 7ae21d8..77c29d1 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T34_75_SortColors.java @@ -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++; + } + + } + + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java index 086b403..2f7e356 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T49_124_MaxPathSum.java @@ -81,4 +81,22 @@ public class T49_124_MaxPathSum { 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; + + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T53_141_HasCycle.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T53_141_HasCycle.java index 929adab..5963972 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T53_141_HasCycle.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T53_141_HasCycle.java @@ -47,4 +47,17 @@ public class T53_141_HasCycle { 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; + } } diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T55_146_LRUCache.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T55_146_LRUCache.java index 1642121..873a1bd 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T55_146_LRUCache.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T55_146_LRUCache.java @@ -1,8 +1,8 @@ package com.markilue.leecode.hot100.second; - import java.util.HashMap; +import java.util.Map; /** *@BelongsProject: Leecode @@ -47,7 +47,7 @@ public class T55_146_LRUCache { Node node = deleteTail(); map.remove(node.key); } - Node node = new Node(key,value); + Node node = new Node(key, value); addToHead(node); map.put(key, node); } else { @@ -95,7 +95,7 @@ public class T55_146_LRUCache { this.val = val; } - public Node(int key ,int val, Node pre, Node next) { + public Node(int key, int val, Node pre, Node next) { this.key = key; this.val = val; this.pre = pre; @@ -116,3 +116,90 @@ public class T55_146_LRUCache { System.out.println(lRUCache.get(4)); // 返回 4 } } + +class LRUCache { + + int capacity; + Node head; + Node tail; + Map 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; + } + + + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T63_206_ReverseList.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T63_206_ReverseList.java new file mode 100644 index 0000000..15e4463 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T63_206_ReverseList.java @@ -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; + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T64_207_CanFinish.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T64_207_CanFinish.java new file mode 100644 index 0000000..68aa13f --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T64_207_CanFinish.java @@ -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> 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 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 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 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;//全都检查完了,没有问题 + } + +}