From e6d23febec800f4d622846681c1f102b35fa7cf3 Mon Sep 17 00:00:00 2001 From: whaifree Date: Thu, 17 Oct 2024 23:22:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EKama99=E5=92=8CLeetCode797?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E5=9B=BE=E5=BD=A2=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加Kama99类,实现了一个算法来计算二维数组中岛屿的数量 - 添加LeetCode797类,实现了寻找所有从源到目标的路径的算法 - 新增p1类,用于解决特定的字符串问题 - 在TestCacheThreadPool中添加了非公平锁的示例代码 - 在ThreadDemo1中添加了广度优先搜索的实现 --- .../java/cn/whaifree/interview/hzyh/p1.java | 24 +++++++ .../cn/whaifree/leetCode/Graph/Kama99.java | 65 +++++++++++++++++++ .../whaifree/leetCode/Graph/LeetCode797.java | 47 ++++++++++++++ .../cn/whaifree/test/TestCacheThreadPool.java | 32 +++++++++ .../java/cn/whaifree/test/ThreadDemo1.java | 31 +++++++++ 5 files changed, 199 insertions(+) create mode 100644 ForJdk17/src/main/java/cn/whaifree/interview/hzyh/p1.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/Kama99.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/LeetCode797.java diff --git a/ForJdk17/src/main/java/cn/whaifree/interview/hzyh/p1.java b/ForJdk17/src/main/java/cn/whaifree/interview/hzyh/p1.java new file mode 100644 index 0000000..d9da32a --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/interview/hzyh/p1.java @@ -0,0 +1,24 @@ +package cn.whaifree.interview.hzyh; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/17 18:23 + * @注释 + */ +public class p1 { + + + /** + * abcdefgihjklmnopqrstuvwxyz + * + * 中包含问号? + * + * 判断最少需要多少长可以涵盖这26个字符,?是万能的。 + * + * @param args + */ + public static void main(String[] args) { + + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/Kama99.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/Kama99.java new file mode 100644 index 0000000..193effe --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/Kama99.java @@ -0,0 +1,65 @@ +package cn.whaifree.leetCode.Graph; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/17 16:26 + * @注释 + */ +public class Kama99 { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int a = scanner.nextInt(); + int b = scanner.nextInt(); + int[][] input = new int[a][b]; + boolean[][] visited = new boolean[a][b]; + for (int i = 0; i < a; i++) { + for (int j = 0; j < b; j++) { + input[i][j] = scanner.nextInt(); + } + } + + int res = 0; + for (int i = 0; i < a; i++) { + for (int j = 0; j < b; j++) { + if (!visited[i][j] && input[i][j] == 1) { + // 没有走过的节点+为陆地(1) + res++; + method(input, visited, i, j); + } + } + } + System.out.println(res); + } + public static int method(int[][] input, boolean[][] looking, int x, int y) { + int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 表示四个方向 + + int res = 0; + int[] item = new int[]{x, y}; + looking[x][y] = true; + Deque queue = new LinkedList<>(); + queue.add(item); + while (!queue.isEmpty()) { + int[] pop = queue.pop(); + int x1 = pop[0]; + int y1 = pop[1]; + for (int i = 0; i < 4; i++) { + int nextX = x1 + dir[i][0]; + int nextY = y1 + dir[i][1]; + if (nextX >= 0 && nextX < input.length && nextY >= 0 && nextY < input[0].length) { + if (!looking[nextX][nextY] && input[nextX][nextY] == 1) { // 只有1才遍历,这样就可以保证只在小岛屿内 + // (下一次的节点)没有遍历过,并且为1, + queue.add(new int[]{nextX, nextY}); + looking[nextX][nextY] = true; // 进入队列就标志看过了 + } + } + } + } + return res; + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/LeetCode797.java b/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/LeetCode797.java new file mode 100644 index 0000000..db960b1 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/leetCode/Graph/LeetCode797.java @@ -0,0 +1,47 @@ +package cn.whaifree.leetCode.Graph; + +import org.junit.Test; + +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/17 18:09 + * @注释 + */ +public class LeetCode797 { + + @Test + public void test() { + int[][] graph = {{4,3,1},{3,2,4},{3},{4},{}}; + List> res = new Solution().allPathsSourceTarget(graph); + res.forEach(System.out::println); + } + + class Solution { + List> res = null; + List path = null; + public List> allPathsSourceTarget(int[][] graph) { + res = new java.util.ArrayList<>(); + path = new java.util.ArrayList<>(); + path.add(0); + dfs(graph, 0); + return res; + } + + public void dfs(int[][] graph, int cur) { + if (!path.isEmpty() && graph.length - 1 == path.get(path.size() - 1)) { + res.add(new java.util.ArrayList<>(path)); + return; + } + + int[] ints = graph[cur]; + for (int i = 0; i < ints.length; i++) { + path.add(ints[i]); + dfs(graph, ints[i]); // 0-4 + path.remove(path.size() - 1); + } + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/test/TestCacheThreadPool.java b/ForJdk17/src/main/java/cn/whaifree/test/TestCacheThreadPool.java index b1f7c70..4eaa7d1 100644 --- a/ForJdk17/src/main/java/cn/whaifree/test/TestCacheThreadPool.java +++ b/ForJdk17/src/main/java/cn/whaifree/test/TestCacheThreadPool.java @@ -271,4 +271,36 @@ public class TestCacheThreadPool { } + + +} +class NoFairLock { + + public static void main(String[] args) { + // 公平锁和非公平锁的主要区别在于,公平锁会检查是否有线程在等待,有就直接封装成Node,而非公平锁无论如何都会去试一下能不能获取锁; + // 公平锁和非公平锁释放的过程是完全一样的,都是通知CLH的后继节点 + ReentrantLock lock = new ReentrantLock(false); + for (int i = 0; i < 15; i++) { + int finalI = i; + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(finalI); + lock.lock(); + System.out.println(Thread.currentThread().getName() + " acquired the lock"); + // 模拟业务操作 + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + System.out.println(Thread.currentThread().getName() + " released the lock"); + lock.unlock(); + } + + } + }, "Thread-" + i).start(); + } + + } } diff --git a/ForJdk17/src/main/java/cn/whaifree/test/ThreadDemo1.java b/ForJdk17/src/main/java/cn/whaifree/test/ThreadDemo1.java index 97af114..0b824b0 100644 --- a/ForJdk17/src/main/java/cn/whaifree/test/ThreadDemo1.java +++ b/ForJdk17/src/main/java/cn/whaifree/test/ThreadDemo1.java @@ -275,6 +275,37 @@ class mockException{ System.out.println(s); } + + private int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 表示四个方向 + /** + * + * @param grid 原地板 + * @param visited 浏览的点 + * @param x 起始位置 + * @param y + */ + public void bfs(char[][] grid, boolean[][] visited, int x, int y) { + Queue queue = new LinkedList<>(); // 定义队列 + queue.offer(new int[]{x, y}); // 起始节点加入队列 + visited[x][y] = true; // 只要加入队列,立刻标记为访问过的节点 + + while (!queue.isEmpty()) { // 开始遍历队列里的元素 + int[] cur = queue.poll(); // 从队列取元素 + int curx = cur[0]; + int cury = cur[1]; // 当前节点坐标 + + for (int i = 0; i < 4; i++) { // 开始想当前节点的四个方向左右上下去遍历 + int nextx = curx + dir[i][0]; + int nexty = cury + dir[i][1]; // 获取周边四个方向的坐标 + if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 坐标越界了,直接跳过 + if (!visited[nextx][nexty]) { // 如果节点没被访问过 + queue.offer(new int[]{nextx, nexty}); // 队列添加该节点为下一轮要遍历的节点 + visited[nextx][nexty] = true; // 只要加入队列立刻标记,避免重复访问 + } + } + } + } + }