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] =?UTF-8?q?feate:=20=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8E=A5?= =?UTF-8?q?=E9=BE=99=20=20Java=E4=BC=98=E5=8C=96=E7=89=88=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98=201.?= =?UTF-8?q?=E4=BD=BF=E7=94=A8null=E6=A0=87=E5=BF=97=E5=88=86=E5=B1=82=202.?= =?UTF-8?q?=E5=AF=BB=E6=89=BE=E9=82=BB=E6=8E=A5=E8=8A=82=E7=82=B9=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=9B=9E=E6=BB=9A=E4=BF=AE=E6=94=B9=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=A4=9A=E6=AC=A1=E6=95=B0=E7=BB=84=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=203.=E4=BF=AE=E5=A4=8D=E8=BE=B9=E7=95=8C=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=9AbeginStr=3D=3DendStr=E6=97=B6=EF=BC=8C=E5=BA=94?= =?UTF-8?q?=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); - } } ```