leecode更新
This commit is contained in:
parent
9eccd2abe2
commit
fea6d3debc
|
|
@ -3,6 +3,8 @@ package com.markilue.leecode.hot100.second;
|
||||||
import com.sun.media.sound.RIFFInvalidDataException;
|
import com.sun.media.sound.RIFFInvalidDataException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@BelongsProject: Leecode
|
*@BelongsProject: Leecode
|
||||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||||
|
|
@ -53,4 +55,28 @@ public class T23_42_Trap {
|
||||||
return sum;
|
return sum;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//接雨水单调栈解法
|
||||||
|
public int trap1(int[] height) {
|
||||||
|
|
||||||
|
LinkedList<Integer> stack = new LinkedList<>();
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < height.length; i++) {
|
||||||
|
while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
|
||||||
|
Integer top = stack.pop();
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
break;//至少要两个才能有积水
|
||||||
|
}
|
||||||
|
Integer left = stack.peek();
|
||||||
|
int curWidth = i - left - 1;
|
||||||
|
int curHeight = Math.min(height[left], height[i])-height[top];
|
||||||
|
result += curHeight * curWidth;
|
||||||
|
}
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -171,4 +171,40 @@ public class T38_84_LargestRectangleArea {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//二刷:单调栈解法
|
||||||
|
public int largestRectangleArea4(int[] heights) {
|
||||||
|
|
||||||
|
int n = heights.length;
|
||||||
|
|
||||||
|
LinkedList<Integer> stack = new LinkedList<>();
|
||||||
|
int[] left = new int[n];
|
||||||
|
int[] right = new int[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {//找第一个小于这个数的
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
left[i] = stack.isEmpty() ? -1 : stack.peek();
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
stack.clear();
|
||||||
|
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {//找第一个小于这个数的
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
right[i] = stack.isEmpty() ? n : stack.peek();
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
int maxArea = Integer.MIN_VALUE;
|
||||||
|
//计算每一个位置最大矩形大小
|
||||||
|
for (int i = 0; i < heights.length; i++) {
|
||||||
|
maxArea = Math.max(maxArea, heights[i] * (right[i] - left[i] - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return maxArea;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
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-10 11:26
|
||||||
|
*@Description: TODO 力扣142 环形链表II
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class T54_142_DetectCycle {
|
||||||
|
|
||||||
|
//快慢指针:如果有环则一定相遇;如果相遇通过数学关系可以推出关系
|
||||||
|
public ListNode detectCycle(ListNode head) {
|
||||||
|
|
||||||
|
ListNode fast = head;
|
||||||
|
ListNode slow = head;
|
||||||
|
|
||||||
|
while (fast != null && fast.next != null) {
|
||||||
|
fast = fast.next.next;
|
||||||
|
slow = slow.next;
|
||||||
|
if (fast == slow) {
|
||||||
|
//两者相遇
|
||||||
|
ListNode start = head;
|
||||||
|
while (start != fast) {
|
||||||
|
start = start.next;
|
||||||
|
fast = fast.next;
|
||||||
|
}
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;//没找到
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,118 @@
|
||||||
|
package com.markilue.leecode.hot100.second;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-05-10 11:42
|
||||||
|
*@Description: TODO 力扣146 LRU缓存
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class T55_146_LRUCache {
|
||||||
|
|
||||||
|
HashMap<Integer, Node> map;
|
||||||
|
Node head;
|
||||||
|
Node tail;
|
||||||
|
int capacity;
|
||||||
|
|
||||||
|
|
||||||
|
public T55_146_LRUCache(int capacity) {
|
||||||
|
this.capacity = capacity;
|
||||||
|
head = new Node();
|
||||||
|
tail = new Node();
|
||||||
|
head.next = tail;
|
||||||
|
tail.pre = head;
|
||||||
|
map = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int get(int key) {
|
||||||
|
Node node = map.get(key);
|
||||||
|
if (node == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
deleteNode(node);
|
||||||
|
addToHead(node);
|
||||||
|
return node.val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(int key, int value) {
|
||||||
|
Node pre = map.get(key);
|
||||||
|
if (pre == null) {
|
||||||
|
if (map.size() == capacity) {
|
||||||
|
Node node = deleteTail();
|
||||||
|
map.remove(node.key);
|
||||||
|
}
|
||||||
|
Node node = new Node(key,value);
|
||||||
|
addToHead(node);
|
||||||
|
map.put(key, node);
|
||||||
|
} else {
|
||||||
|
pre.val = value;
|
||||||
|
deleteNode(pre);
|
||||||
|
addToHead(pre);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node deleteTail() {
|
||||||
|
Node delete = tail.pre;
|
||||||
|
deleteNode(tail.pre);
|
||||||
|
return delete;
|
||||||
|
}
|
||||||
|
|
||||||
|
//将节点加在头部
|
||||||
|
private void addToHead(Node node) {
|
||||||
|
node.next = head.next;
|
||||||
|
head.next.pre = node;
|
||||||
|
head.next = node;
|
||||||
|
node.pre = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
//删除当前节点
|
||||||
|
private void deleteNode(Node node) {
|
||||||
|
node.next.pre = node.pre;
|
||||||
|
node.pre.next = node.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
Node pre;
|
||||||
|
Node next;
|
||||||
|
int val;
|
||||||
|
int key;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int key, int val) {
|
||||||
|
this.key = key;
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int key ,int val, Node pre, Node next) {
|
||||||
|
this.key = key;
|
||||||
|
this.val = val;
|
||||||
|
this.pre = pre;
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
T55_146_LRUCache lRUCache = new T55_146_LRUCache(2);
|
||||||
|
lRUCache.put(1, 1); // 缓存是 {1=1}
|
||||||
|
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
|
||||||
|
System.out.println(lRUCache.get(1)); // 返回 1
|
||||||
|
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
|
||||||
|
System.out.println(lRUCache.get(2)); // 返回 -1 (未找到)
|
||||||
|
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
|
||||||
|
System.out.println(lRUCache.get(1)); // 返回 -1 (未找到)
|
||||||
|
System.out.println(lRUCache.get(3)); // 返回 3
|
||||||
|
System.out.println(lRUCache.get(4)); // 返回 4
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue