From 91137dc297f7a9ca7abb2d59fd9c45afe04f2366 Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Thu, 3 Oct 2024 19:39:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20Errata=20=E8=BE=93=E5=87=BA=E8=BE=93?= =?UTF-8?q?=E5=87=BA->=E8=BE=93=E5=85=A5=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0098.所有可达路径.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/kamacoder/0098.所有可达路径.md b/problems/kamacoder/0098.所有可达路径.md index db35524f..a2f60a90 100644 --- a/problems/kamacoder/0098.所有可达路径.md +++ b/problems/kamacoder/0098.所有可达路径.md @@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候,要关注看看格式问题,特别 有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了? -ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输出输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 +ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输入输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释) From 4eef0863a293b7f2cfd36214a3281ae26093022e Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Fri, 4 Oct 2024 21:18:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=200100.=E5=B2=9B=E5=B1=BF=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=9D=A2=E7=A7=AF=20Java=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kamacoder/0100.岛屿的最大面积.md | 123 +++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/problems/kamacoder/0100.岛屿的最大面积.md b/problems/kamacoder/0100.岛屿的最大面积.md index 51bfc57f..871925df 100644 --- a/problems/kamacoder/0100.岛屿的最大面积.md +++ b/problems/kamacoder/0100.岛屿的最大面积.md @@ -222,7 +222,128 @@ public: ## 其他语言版本 -### Java +### Java + +```java +import java.util.*; +import java.math.*; + +/** + * DFS版 + */ +public class Main{ + + static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}}; + static int result=0; + static int count=0; + + public static void main(String[] args){ + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j]=scanner.nextInt(); + } + } + boolean[][] visited = new boolean[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if(!visited[i][j]&&map[i][j]==1){ + count=0; + dfs(map,visited,i,j); + result= Math.max(count, result); + } + } + } + System.out.println(result); + } + + static void dfs(int[][] map,boolean[][] visited,int x,int y){ + count++; + visited[x][y]=true; + for (int i = 0; i < 4; i++) { + int nextX=x+dir[i][0]; + int nextY=y+dir[i][1]; + //水或者已经访问过的跳过 + if(nextX<0||nextY<0 + ||nextX>=map.length||nextY>=map[0].length + ||visited[nextX][nextY]||map[nextX][nextY]==0)continue; + + dfs(map,visited,nextX,nextY); + } + } +} +``` + +```java +import java.util.*; +import java.math.*; + +/** + * BFS版 + */ +public class Main { + static class Node { + int x; + int y; + + public Node(int x, int y) { + this.x = x; + this.y = y; + } + } + + static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + static int result = 0; + static int count = 0; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j] = scanner.nextInt(); + } + } + boolean[][] visited = new boolean[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && map[i][j] == 1) { + count = 0; + bfs(map, visited, i, j); + result = Math.max(count, result); + } + } + } + System.out.println(result); + } + + static void bfs(int[][] map, boolean[][] visited, int x, int y) { + Queue q = new LinkedList<>(); + q.add(new Node(x, y)); + visited[x][y] = true; + count++; + while (!q.isEmpty()) { + Node node = q.remove(); + for (int i = 0; i < 4; i++) { + int nextX = node.x + dir[i][0]; + int nextY = node.y + dir[i][1]; + if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0) + continue; + q.add(new Node(nextX, nextY)); + visited[nextX][nextY] = true; + count++; + } + } + } +} + +``` + ### Python From 9c7131e253ad9b59aaa69f102f03e0154752b660 Mon Sep 17 00:00:00 2001 From: Jasonyou <2926593225@qq.com> Date: Sun, 6 Oct 2024 03:04:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feate:=20=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=8E=A5=E9=BE=99=20=20Java=E4=BC=98=E5=8C=96=E7=89=88?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=201.=E4=BD=BF=E7=94=A8null=E6=A0=87=E5=BF=97=E5=88=86=E5=B1=82?= =?UTF-8?q?=202.=E5=AF=BB=E6=89=BE=E9=82=BB=E6=8E=A5=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=9B=9E=E6=BB=9A=E4=BF=AE=E6=94=B9=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=A4=9A=E6=AC=A1=E6=95=B0=E7=BB=84=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=203.=E4=BF=AE=E5=A4=8D=E8=BE=B9=E7=95=8C=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=EF=BC=9AbeginStr=3D=3DendStr=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=BA=94=E8=AF=A5=E8=BE=93=E5=87=BA0=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0110.字符串接龙.md | 106 +++++++++++---------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/problems/kamacoder/0110.字符串接龙.md b/problems/kamacoder/0110.字符串接龙.md index 308e50b7..ef261c4d 100644 --- a/problems/kamacoder/0110.字符串接龙.md +++ b/problems/kamacoder/0110.字符串接龙.md @@ -152,66 +152,70 @@ int main() { ## 其他语言版本 -### Java +### Java + ```Java +import java.util.*; + public class Main { - // BFS方法 - public static int ladderLength(String beginWord, String endWord, List wordList) { - // 使用set作为查询容器,效率更高 - HashSet set = new HashSet<>(wordList); - - // 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串 - Queue queue = new LinkedList<>(); - - // 声明一个hashMap存储遍历到的字符串以及所走过的路径path - HashMap visitMap = new HashMap<>(); - queue.offer(beginWord); - visitMap.put(beginWord, 1); - - while (!queue.isEmpty()) { - String curWord = queue.poll(); - int path = visitMap.get(curWord); + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + scanner.nextLine(); + String beginStr = scanner.next(); + String endStr = scanner.next(); + scanner.nextLine(); + List wordList = new ArrayList<>(); + wordList.add(beginStr); + wordList.add(endStr); + for (int i = 0; i < n; i++) { + wordList.add(scanner.nextLine()); + } + int count = bfs(beginStr, endStr, wordList); + System.out.println(count); + } - for (int i = 0; i < curWord.length(); i++) { - char[] ch = curWord.toCharArray(); - // 每个位置尝试26个字母 - for (char k = 'a'; k <= 'z'; k++) { - ch[i] = k; - - String newWord = new String(ch); - if (newWord.equals(endWord)) return path + 1; - - // 如果这个新字符串存在于容器且之前未被访问到 - if (set.contains(newWord) && !visitMap.containsKey(newWord)) { - visitMap.put(newWord, path + 1); - queue.offer(newWord); + /** + * 广度优先搜索-寻找最短路径 + */ + public static int bfs(String beginStr, String endStr, List wordList) { + int len = 1; + Set set = new HashSet<>(wordList); + Set visited = new HashSet<>(); + Queue q = new LinkedList<>(); + visited.add(beginStr); + q.add(beginStr); + q.add(null); + while (!q.isEmpty()) { + String node = q.remove(); + //上一层结束,若下一层还有节点进入下一层 + if (node == null) { + if (!q.isEmpty()) { + len++; + q.add(null); + } + continue; + } + char[] charArray = node.toCharArray(); + //寻找邻接节点 + for (int i = 0; i < charArray.length; i++) { + //记录旧值,用于回滚修改 + char old = charArray[i]; + for (char j = 'a'; j <= 'z'; j++) { + charArray[i] = j; + String newWord = new String(charArray); + if (set.contains(newWord) && !visited.contains(newWord)) { + q.add(newWord); + visited.add(newWord); + //找到结尾 + if (newWord.equals(endStr)) return len + 1; } } + charArray[i] = old; } } - return 0; } - - public static void main (String[] args) { - /* code */ - // 接收输入 - Scanner sc = new Scanner(System.in); - int N = sc.nextInt(); - sc.nextLine(); - String[] strs = sc.nextLine().split(" "); - - List wordList = new ArrayList<>(); - for (int i = 0; i < N; i++) { - wordList.add(sc.nextLine()); - } - - // wordList.add(strs[1]); - - // 打印结果 - int result = ladderLength(strs[0], strs[1], wordList); - System.out.println(result); - } } ```