最短单词路径

127. Word Ladder (Medium)

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

题目描述:

  找出一条从beginword到endword的最短路径,每次移动规定为改变一个字符,并且改变之后的字符串必须在 wordList 中。

思路分析:

  采用BFS的思路找最短路径。

代码:

class Solution {
public int ladderLength(String beginWord,String endWord,List<String>wordList){
if(beginWord==null||endWord==null||beginWord.equals(endWord)||!wordList.contains(endWord))
return 0;
Queue<String>q=new LinkedList<>();//构造队列辅助BFS
Set<String>visited=new HashSet<>(); //标记串是否已访问过
Set<String>dict=new HashSet<>(wordList);//wordList中可能出现重复的串
q.offer(beginWord);
visited.add(beginWord);
int len=1;
while(!q.isEmpty()){
int size=q.size(); //当前队列中字符串的个数
for(int s=0;s<size;s++){
String cur=q.poll();
for(int i=0;i<cur.length();i++){ //对当前字符串的每一位进行改变
for(char c='a';c<='z';c++){ //搜索的方式
char []curArray=cur.toCharArray();
char c1=curArray[i];
curArray[i]=c;
String temp=new String(curArray);
if(temp.equals(endWord)){ //到达endword
return len+1;
}
if(!visited.contains(temp)&&dict.contains(temp)){
visited.add(temp);
q.offer(temp);
}
curArray[i]=c1;//每次只能修改一个字母,所以为了进行下一个位置的搜索,需要还原当前位置的字符。
} }
}
len++; //每进行一次大的循环,长度加一。
}
return 0;
}
}

搜索(BFS)---最短单词路径的更多相关文章

  1. 【算法入门】广度/宽度优先搜索(BFS)

    广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...

  2. 【数据结构与算法Python版学习笔记】图——词梯问题 广度优先搜索 BFS

    词梯Word Ladder问题 要求是相邻两个单词之间差异只能是1个字母,如FOOL变SAGE: FOOL >> POOL >> POLL >> POLE > ...

  3. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  5. 【洛谷P2384】最短乘积路径

    题目大意:给定 N 个点,M 条边的有向图,边有边权,求从 1 号顶点到 N 号顶点的最短乘积路径.(经过的路径乘积最小)结果对9987取模. 乘积会爆 long long ,同时由于 dij 算法的 ...

  6. 广度优先搜索 BFS算法

    广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...

  7. 广度优先搜索 BFS 学习笔记

    广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...

  8. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  9. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

随机推荐

  1. 【leetcode】1160. Find Words That Can Be Formed by Characters

    题目如下: You are given an array of strings words and a string chars. A string is good if it can be form ...

  2. Java——IO

    [Java流式输入/输出原理]  

  3. 【CF686D】Kay and Snowflake(树的重心)

    题意:给定一棵n个点的树,q次询问,每次询问以某个点为根的子树编号是多少 n,q<=3e5 思路:设sz[u]为以u为根子树的size,v为u的size最大的儿子 若sz[v]*2<sz[ ...

  4. C# 很久以前几个常用类

    Base64加密解密 using System; using System.Collections.Generic; using System.Linq; using System.Text; nam ...

  5. E. Compress Words

    E. Compress Words KMP #include<bits/stdc++.h> using namespace std; ]; int len; void getNext(ch ...

  6. 嵌入QQ聊天

    <a href="http://wpa.qq.com/msgrd?V=1&Uin=1178321443&Site=http://www.nanfangjiadian.c ...

  7. testlink用例转换工具2018.12版

    首先说明一点,网上有很多资料,但真正可用的很少:在本人经过百度后,发现其实很多案例会因为各种原因而无法最终实现. Testlink用例转换工具,可以大致分为3种工具: 1)EX-Converter由第 ...

  8. 《Vue前端开发手册》

    序言 为了统一前端的技术栈问题,技术开发二部规定开发技术必须以Vue为主. 为了更好的规范公司的前端框架,现以我前端架构师为主,编写以下开发规范,如有不当的地方,欢迎批评教育并慢慢改善该开发文档,谢谢 ...

  9. Linux_LAMP 最强大的动态网站解决方案

    目录 目录 LAMP Install LAMP via YUM Install LAMP via ResourceCode Apache Apache Virtual Machine Type Use ...

  10. Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试

    窗体上需要 TImage.TOpenDialog 和六个按钮. unit Unit1; interface uses   System.SysUtils, System.Types, System.U ...