当前位置:网站首页>126. 单词接龙 II BFS
126. 单词接龙 II BFS
2022-06-24 09:47:00 【钰娘娘】
126. 单词接龙 II
按字典
wordList完成从单词beginWord到单词endWord转化,一个表示此过程的 转换序列 是形式上像beginWord -> s1 -> s2 -> ... -> sk这样的单词序列,并满足:
- 每对相邻的单词之间仅有单个字母不同。
- 转换过程中的每个单词
si(1 <= i <= k)必须是字典wordList中的单词。注意,beginWord不必是字典wordList中的单词。sk == endWord给你两个单词
beginWord和endWord,以及一个字典wordList。请你找出并返回所有从beginWord到endWord的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表[beginWord, s1, s2, ..., sk]的形式返回。示例 1:
输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] 输出:[["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]] 解释:存在 2 种最短的转换序列: "hit" -> "hot" -> "dot" -> "dog" -> "cog" "hit" -> "hot" -> "lot" -> "log" -> "cog"示例 2:
输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] 输出:[] 解释:endWord "cog" 不在字典 wordList 中,所以不存在符合要求的转换序列。提示:
1 <= beginWord.length <= 5endWord.length == beginWord.length1 <= wordList.length <= 500wordList[i].length == beginWord.lengthbeginWord、endWord和wordList[i]由小写英文字母组成beginWord != endWordwordList中的所有单词 互不相同来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-ladder-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
失败,反复调BFS,DFS,还是不通过超时
方法:BFS
此题卡的很严,疯狂TLE,没有优化是过不了的,看过题解理解后自己改成了自己容易理解的方式
1. 特判,终点不在列表返回
2. 反图,方便倒着找回起点;如果是正图,可能存在死路,造成额外耗时,反图也是一种剪枝方式
3. 检查是否存在 a->b 的通路,如果 b 点是首次访问,还需要搜索 b 为终点的反图,否则需要看是否步数相同,如果步数相同,也可作为到达此点的方案之一
4. 如果这一层结束后,已经到达终点,无需继续找,后续步数会增加,不会是最佳答案,直接结束
5. 可能找不到可行路径,返回空
6. 可找到的就反向找,通过双端队列插入头部,获得正向解(或者你直接用正常列表反过来也行)
public class Solution {
List<List<String>> ans = new ArrayList<>();
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Set<String> nodes = new HashSet<>(wordList);
//终点不在wordList返回空
if(!nodes.contains(endWord)) return ans;
Map<String,Set<String>> reGraph = new HashMap<>();//反图
Set<String> orgin = new HashSet<>(wordList);
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
//假设长度为 x 已经有可行路径,无需再查长度 x+1 的
while (!queue.isEmpty() && !reGraph.containsKey(endWord)){
int size = queue.size();
//同层节点
Set<String> line = new HashSet<>();
//层序找下一个点
for(int i = 0; i < size; i++){
String a = queue.poll();
//原始点列表,找所有下一个可访问点
for(String b:orgin){
if(!diffOne(a,b)) continue;
//如果刚好前面的步数和现在一样(同一层)或者首次访问
if(line.contains(b) || nodes.contains(b)){
reGraph.putIfAbsent(b,new HashSet<>());
reGraph.get(b).add(a);
line.add(b);
//首次访问删节点,继续找路
if(nodes.remove(b)){
queue.offer(b);
}
}
}
}
}
//需判断是否可行路径
if(!reGraph.isEmpty()){
Deque<String> deque = new LinkedList<>();
deque.push(endWord);
dfs(reGraph,deque,beginWord,endWord);
}
return ans;
}
private void dfs(Map<String,Set<String>> graph, Deque<String> deque,String start,String end){
if(start.equals(end)){
ans.add(new ArrayList<>(deque));
return;
}
for(String pre:graph.get(end)){
deque.addFirst(pre);
dfs(graph,deque,start,pre);
deque.removeFirst();
}
}
private boolean diffOne(String a, String b){
int n = a.length();
int diff = 0;
for(int i = 0; i < n&&diff<2; i++){
if(a.charAt(i)!=b.charAt(i)) ++diff;
}
return diff == 1;
}
}边栏推荐
- 【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
- 线程的六种状态
- A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
- How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
- 学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
- 【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
- Leetcode interview question 16.06: minimum difference
- 5.菜品管理业务开发
- Development of anti fleeing marketing software for health products
- SQL Server AVG函数取整问题
猜你喜欢

p5.js千纸鹤动画背景js特效

leetCode-1823: 找出游戏的获胜者

利用pandas读取SQL Sever数据表

Using pandas to read SQL server data table

Uniapp implements the function of clicking to make a call

分布式系统你必须了解的点-CAP

Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default

分布式事务原理以及解决分布式事务方案

4. classification management business development

6.套餐管理业务开发
随机推荐
Learning to organize using kindeditor rich text editor in PHP
dedecms模板文件讲解以及首页标签替换
抓包工具charles實踐分享
Leetcode-1823: find the winner of the game
uniapp实现禁止video拖拽快进
抓包工具charles实践分享
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
线程调度的常用方法
Machine learning perceptron and k-nearest neighbor
顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
Distributed | how to make "secret calls" with dble
学习使用phpstripslashe函数去除反斜杠
Sort out interface performance optimization skills and kill slow code
【JS逆向分享】某个网站社区信息
5. dish management business development
2022年能源与环境工程国际研讨会(CoEEE 2022)
JMeter接口测试工具基础 — Badboy工具
Development of anti fleeing marketing software for health products
Leetcode - 498: traversée diagonale