feat(redo): 添加 LRU缓存、最小栈和岛屿数量算法的实现

- 新增 LeetCode146 类,实现 LRU 缓存算法
- 新增 LeetCode155 类,实现最小栈数据结构
- 新增 LeetCode200 类,解决岛屿数量问题
This commit is contained in:
whai 2024-11-04 12:23:06 +08:00
parent eadd391047
commit 9e8153e434
4 changed files with 322 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package cn.whaifree.leetCode;
import org.junit.Test;
import java.security.PublicKey;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/11/4 11:27
* @注释
*/
public class LeetCode200 {
@Test
public void test() {
char[][] grid = {{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'}};
Solution solution = new Solution();
int res = solution.numIslands(grid);
System.out.println(res);
}
class Solution {
public int numIslands(char[][] grid) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
int res = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
// 没遍历过的陆地
if (grid[i][j] == '1' && !visited[i][j]) {
visited[i][j] = true;
dfs(grid, visited, i, j);
res++;
}
}
}
return res;
}
int[][] direct = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
public void dfs(char[] [] grid, boolean[][] visited, int i, int j) {
for (int x = 0; x < direct.length; x++) {
int nextX = i + direct[x][0];
int nextY = j + direct[x][1];
if (nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length) {
if (grid[nextX][nextY] == '1' && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
dfs(grid, visited, nextX, nextY);
}
}
}
}
public void wfs(char[] [] grid, boolean[][] visited, int i, int j) {
}
}
}

View File

@ -0,0 +1,183 @@
package cn.whaifree.redo.redo_all_241016;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/11/4 10:26
* @注释
*/
public class LeetCode146 {
@Test
public void test() {
LRUCache lruCache = new LRUCache(2);
lruCache.put(1,1);
lruCache.put(2,2);
System.out.println(lruCache.get(1));
lruCache.put(3,3);
System.out.println(lruCache.get(2));
lruCache.put(4,4);
System.out.println(lruCache.get(1));
System.out.println(lruCache.get(3));
System.out.println(lruCache.get(4));
}
class LRUCache {
static class ItemNode <K,V> {
ItemNode<K,V> pre;
ItemNode<K,V> next;
K key;
V value;
public ItemNode(K key, V value) {
this.key = key;
this.value = value;
}
public ItemNode() {
}
}
Map<Integer, ItemNode<Integer,Integer>> map;
int capacity;
ItemNode<Integer, Integer> head;
ItemNode<Integer, Integer> tail;
public LRUCache(int capacity) {
map = new HashMap<>();
this.capacity = capacity;
head = new ItemNode<>();
tail = new ItemNode<>();
head.next = tail;
tail.pre = head;
}
// 添加到头部
public void addToHead(ItemNode<Integer, Integer> itemNode) {
ItemNode<Integer, Integer> tmp = head.next;
head.next = itemNode;
itemNode.pre = head;
itemNode.next = tmp;
tmp.pre = itemNode;
}
public void removeItem(ItemNode<Integer, Integer> itemNode) {
itemNode.pre.next = itemNode.next;
itemNode.next.pre = itemNode.pre;
}
public int get(int key) {
if (!map.containsKey(key)) {
return -1;
}
ItemNode<Integer, Integer> getThisNode = map.get(key);
// 删除该节点
removeItem(getThisNode);
// 添加到头部
addToHead(getThisNode);
return getThisNode.value;
}
public void put(int key, int value) {
if (map.containsKey(key)) {
ItemNode<Integer, Integer> origin = map.get(key);
origin.value = value;
get(key);
} else {
ItemNode<Integer, Integer> newItem = new ItemNode<>(key, value);
map.put(key, newItem);
addToHead(newItem);
while (map.size() > capacity) {
ItemNode<Integer, Integer> del = tail.pre;
Integer delK = del.key;
map.remove(delK);
removeItem(del);
}
}
}
}
// class LRUCache {
// static class ItemNode{
// ItemNode pre;
// ItemNode next;
// Integer key;
// Integer value;
//
// public ItemNode(Integer key, Integer value) {
// this.key = key;
// this.value = value;
// }
//
// public ItemNode() {
// }
// }
//
// Map<Integer, ItemNode> map;
// int capacity;
// ItemNode head;
// ItemNode tail;
//
//
// public LRUCache(int capacity) {
// map = new HashMap<>();
// this.capacity = capacity;
// head = new ItemNode();
// tail = new ItemNode();
// head.next = tail;
// tail.pre = head;
// }
//
// // 添加到头部
// public void addToHead(ItemNode itemNode) {
// ItemNode tmp = head.next;
// head.next = itemNode;
// itemNode.pre = head;
// itemNode.next = tmp;
// tmp.pre = itemNode;
// }
//
// public void removeItem(ItemNode itemNode) {
// itemNode.pre.next = itemNode.next;
// itemNode.next.pre = itemNode.pre;
// }
//
// public int get(int key) {
// if (!map.containsKey(key)) {
// return -1;
// }
// ItemNode getThisNode = map.get(key);
// // 删除该节点
// removeItem(getThisNode);
// // 添加到头部
// addToHead(getThisNode);
// return getThisNode.value;
// }
//
// public void put(int key, int value) {
// if (map.containsKey(key)) {
// ItemNode origin = map.get(key);
// origin.value = value;
// get(key);
// } else {
// ItemNode newItem = new ItemNode(key, value);
// map.put(key, newItem);
// addToHead(newItem);
// while (map.size() > capacity) {
// ItemNode del = tail.pre;
// Integer delK = del.key;
// map.remove(delK);
// removeItem(del);
// }
// }
// }
// }
}

View File

@ -0,0 +1,69 @@
package cn.whaifree.redo.redo_all_241016;
import org.junit.Test;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/11/4 11:10
* @注释
*/
public class LeetCode155 {
@Test
public void test() {
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.top());
System.out.println(minStack.getMin());
}
class MinStack {
Deque<int[]> deque;
public MinStack() {
deque = new ArrayDeque<>();
}
public void push(int val) {
if (deque.isEmpty()) {
deque.add(new int[]{val, val});
}else {
int[] peek = deque.peek();
int min = peek[1];
if (min > val) {
min = val;
}
deque.push(new int[]{val, min});
}
}
public void pop() {
deque.pop();
}
public int top() {
if (deque.isEmpty()) {
return -1;
}
int[] peek = deque.peek();
return peek[0];
}
public int getMin() {
if (deque.isEmpty()) {
return -1;
}
return deque.peek()[1];
}
}
}