【leetcode】No.126:word-ladder-Ⅱ

題目描述

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,

Given:

start ="hit"
end ="cog"
dict =["hot","dot","dog","lot","log"]

Return:
  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:
All words have the same length.
All words contain only lowercase alphabetic characters.

思路:

與上一題不同,這次需要存儲(chǔ)所有的最短路徑,就不能在入隊(duì)前把字典中的節(jié)點(diǎn)除去。我們需要一個(gè)隊(duì)列存儲(chǔ)路徑,每次取出隊(duì)列頭的路徑,把隊(duì)頭路徑的最后一個(gè)節(jié)點(diǎn)拿出來尋找下一個(gè)可達(dá)的節(jié)點(diǎn)并存入set中,如果可達(dá)節(jié)點(diǎn)為end節(jié)點(diǎn),則將路徑保存進(jìn)結(jié)果列表中,如果不為end節(jié)點(diǎn),則生成新的路徑存入隊(duì)尾。如果路徑長(zhǎng)度大于當(dāng)前路徑長(zhǎng)度(也就是層數(shù)),則更新當(dāng)前路徑長(zhǎng)度,同時(shí)將set中節(jié)點(diǎn)都從字典中清除,因?yàn)檫@些節(jié)點(diǎn)都是上層的節(jié)點(diǎn),我們不希望再走回頭路,這肯定不是最短路徑了。如果發(fā)現(xiàn)當(dāng)前路徑長(zhǎng)度已經(jīng)大于到達(dá)end節(jié)點(diǎn)的路徑長(zhǎng)度了,則后面的路徑都不是最短路徑了,直接break。

代碼:

import java.util.*;

public class Solution {
    public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
        ArrayList<ArrayList<String>> res = new ArrayList();
        Queue<ArrayList<String>> paths = new LinkedList();
        ArrayList<String> startPath = new ArrayList();
        startPath.add(start);
        paths.offer(startPath);
        HashSet<String> nodeToMove = new HashSet();
        int level = 1, lastLevel = Integer.MAX_VALUE;
        while (!paths.isEmpty()){
            ArrayList<String> path = paths.poll();
            // 如果當(dāng)前頭部的路徑長(zhǎng)度已經(jīng)超過level,說明已經(jīng)到了下一層,之前的節(jié)點(diǎn)都可以清除了
            if (path.size() > level){
                // 從字典中清除節(jié)點(diǎn)
                dict.removeAll(nodeToMove);
                // 清除記錄的節(jié)點(diǎn)
                nodeToMove.clear();
                // 更新路徑長(zhǎng)度
                level = path.size();
                // 如果長(zhǎng)度大于到達(dá)終點(diǎn)的最短路徑,則退出循環(huán)
                if (level > lastLevel) break;
            }
            // 拿到頭部路徑的最后一個(gè)節(jié)點(diǎn)
            String last = path.get(path.size() - 1);
            char str[] = last.toCharArray();
            // 尋找下一個(gè)節(jié)點(diǎn)
            for (int i=0; i<str.length; i++){
                char original = str[i];
                for (char c='a'; c<='z'; c++){
                    str[i] = c;
                    String next = new String(str);
                    // 如果節(jié)點(diǎn)在字典中存在
                    if (dict.contains(next)){
                        // 記錄要清除的節(jié)點(diǎn)
                        nodeToMove.add(next);
                        // 創(chuàng)建新的路徑并把下一個(gè)節(jié)點(diǎn)加入
                        ArrayList<String> nextPath = new ArrayList(path);
                        nextPath.add(next);
                        // 如果已經(jīng)到達(dá)終點(diǎn)則加入結(jié)果的列表中,并記錄最短路徑長(zhǎng)度
                        if (next.equals(end)){
                            res.add(nextPath);
                            lastLevel = level;
                        }
                        // 否則將路徑加入隊(duì)列的隊(duì)尾
                        else {
                            paths.offer(nextPath);
                        }
                    }
                    str[i] = original;
                }
            }
        }
        return res;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容