Leetcode 126.单词接龙II
单词接龙II
给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:
- 每次转换只能改变一个字母。
- 转换过程中的中间单词必须是字典中的单词。
说明:
- 如果不存在这样的转换序列,返回一个空列表。
- 所有单词具有相同的长度。
- 所有单词只由小写字母组成。
- 字典中不存在重复的单词。
- 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
输出:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
示例 2:
输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
输出: []
解释: endWord "cog" 不在字典中,所以不存在符合要求的转换序列。
先BFS遍历图,找到所有到达结尾单词的最短路径,同时记录图。最后再DFS遍历图。
BFS遍历图:queue用来BFS遍历图。队列按结点的深度依次存放待处理的结点。首先存放第一层结点,弹出,根据第一层结点找到所有第二层结点放入队列;弹出第二层某个结点,根据此结点找到所有第三层结点放入队列,以此类推。
记录图:记录图中每个结点的父节点们。记录图中结点的层数(深度)。
DFS遍历记录的图得出结果。
class Solution{
public boolean isDiffOneWord(String a,String b){
int diffnum=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i)!=b.charAt(i)){
diffnum++;
}
if(diffnum==2){
return false;
}
}
if(diffnum==1){
return true;
}
return false;
} public List<List<String>> findLadders(String beginWord,String endWord,List<String> wordList){
List<List<String>> res=new ArrayList<List<String>>();
int endIndex=wordList.indexOf(endWord);
if(endIndex==-1){
return res;
}
int beginIndex=wordList.indexOf(beginWord);
//若beginWord不在wordList中,则添加至wordList末尾
if(beginIndex==-1){
wordList.add(beginWord);
beginIndex=wordList.size()-1;
}
//queue用来BFS遍历图。队列按节点的深度依次存放待处理的节点。首先存放第一层结点,弹出
//根据第一层结点找到所有第二层结点放入队列;以此类推
Queue<Integer> queue=new LinkedList<Integer>();
queue.offer(beginIndex);
//fatherNodes、height用来记录图
//记录图中每个节点的父亲节点们
Map<Integer,List<Integer>> fatherNodes=new HashMap<Integer,List<Integer>>();
for(int i=0;i<wordList.size();i++){
fatherNodes.put(i,new ArrayList<Integer>());
}
//记录图中节点的层数(深度)
int[] height=new int[wordList.size()];
height[beginIndex]=1;
while(!queue.isEmpty()){
int nowIndex=queue.poll();
//若最短深度的路径已经记录完毕,退出循环
//height[nowIndex]>=height[endIndex]针对多个父亲点的情况
if(height[endIndex]!=0 && height[nowIndex]>=height[endIndex]){
break;
}
for(int i=0;i<wordList.size();i++){
//height[i]==0未访问过的节点,height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
if((height[i]==0 || height[i]==height[nowIndex]+1) && isDiffOneWord(wordList.get(nowIndex),wordList.get(i))){
if(height[i]==0){
queue.offer(i);
height[i]=height[nowIndex]+1;
fatherNodes.get(i).add(nowIndex);
}else{
//height[i]=height[nowIndex]+1多个父亲节点的情况,且一个父亲节点已经访问过该结点
fatherNodes.get(i).add(nowIndex);
}
}
}
} if(height[endIndex]==0){
return res;
}else{
List<String> list=new ArrayList<String>();
list.add(wordList.get(endIndex));
dfs(endIndex,list,res,wordList,fatherNodes,beginIndex);
}
return res;
} public void dfs(int lastIndex,List<String> list,List<List<String>> res,List<String> wordList,Map<Integer,List<Integer>> fatherNodes,int beginIndex){
if(lastIndex==beginIndex){
List<String> newList=new ArrayList<String>(list);
Collections.reverse(newList);
res.add(newList);
return;
}
for(int i=0;i<fatherNodes.get(lastIndex).size();i++){
int fatherIndex=fatherNodes.get(lastIndex).get(i);
list.add(wordList.get(fatherIndex));
dfs(fatherIndex,list,res,wordList,fatherNodes,beginIndex);
list.remove(list.size()-1);
}
}
}
Leetcode 126.单词接龙II的更多相关文章
- Java实现 LeetCode 126 单词接龙 II
126. 单词接龙 II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: ...
- [LeetCode] 126. 单词接龙 II
题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...
- 126. 单词接龙 II
题目: 链接:https://leetcode-cn.com/problems/word-ladder-ii/ 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出 ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- Java实现 LeetCode 127 单词接龙
127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...
- [Swift]LeetCode126. 单词接龙 II | Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- lintcode 单词接龙II
题意 给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列 比如: 1.每次只能改变一个字母. 2.变换过程中的中间单词必须在字典中出现. 注意事项 所有单词具有相 ...
- leetcode 127 单词接龙
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...
- leetcode 137单词接龙
直接层序遍历,结果有部分测试样例超时: class Solution { public: int ladderLength(string beginWord, string endWord, vect ...
随机推荐
- js 本地存储 localStorage 之 angular
今天项目中用到 php yii框架 用的不是 angular路由 所以用rootScope传值是不行的 我就用到了 localStorage 本地持久化存储 如下 set 顾名思义是设置 值 loca ...
- 关于Anaconda环境变量配置遇到的一些情况说明
安装和配置环境变量的话就不多说了,大家可以参照这个说的去做就行 https://blog.csdn.net/weixin_42997646/article/details/89414769 验证配置环 ...
- 【杂文】5亿大质数表(5e8)
[杂文]\(5\) 亿大质数表(\(5e8\)) 在写哈希,刷数论题时曾一度想要查质数,\(F**k\) 百度文库数据又少,翻页蛋疼,还不给复制,真的是服了. 于是在我闲的蛋疼的时候就搞了个质数表出来 ...
- WMI 技术
什么是WMI? Windows Management Instrumentation (WMI)是可伸缩的系统管理结构,该规范采用一个统一.基于标准且可扩展的面向对象接口.它提供与系统管理员信息和基础 ...
- CentOS 7下ElasticSearch集群搭建案例
最近在网上看到很多ElasticSearch集群的搭建方法,本人在这人使用Elasticsearch5.0.1版本,介绍如何搭建ElasticSearch集群并安装head插件和其他插件安装方法. 一 ...
- c++利用jsoncpp libcurl 构造http 包(原)
我们手游要接入uc九游进行测试,要用http向uc那边的sdk 服务器post json数据. 虽然他们提供了php,java还有c#的服务端demo,但是我们服务器是C++写的,我实在不想中间再转 ...
- 文档声明和HTML样式表
文档声明 不是注释也不是元素,总是在HTML的第一行 书写格式:<!DOCTYPE HTML> 是用于通知浏览器目前文档正使用哪一个HTML版本(相关属性 lang) 若不写文档声明,浏览 ...
- Java线程及Jvm监控工具
Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...
- windows系统下的redis启动教程
下载解压后配置redis.conf文件配置端口号和密码,打开poweshell命令,进入redis解压目录,使用.\redis-server.exe redis.conf 命令启动redis服务,再打 ...
- Farseer.net轻量级开源框架 中级篇:数据库切换
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 动态数据库访问 下一篇:Farseer.net轻量级开源框架 中级篇: SQL执行 ...