Leetcode 126.单词接龙II
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 流量计选型的七大要点
- 下一篇: 金属转子流量计选型注意事项