diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode200.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode200.java new file mode 100644 index 0000000..2d52f87 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/LeetCode200.java @@ -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) { + + + } + + + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode146.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode146.java new file mode 100644 index 0000000..db403ad --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode146.java @@ -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 { + ItemNode pre; + ItemNode next; + K key; + V value; + + public ItemNode(K key, V value) { + this.key = key; + this.value = value; + } + + public ItemNode() { + } + } + + Map> 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); + } + } + } + } + +// 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 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); +// } +// } +// } +// } + + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode155.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode155.java new file mode 100644 index 0000000..1e4d918 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode155.java @@ -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 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]; + } + } + +} diff --git a/ForJdk17/src/main/resources/LeetCode215.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode215.java similarity index 100% rename from ForJdk17/src/main/resources/LeetCode215.java rename to ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode215.java