成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode] Word Ladder 單詞爬梯

pinecone / 2610人閱讀

摘要:另外,為了避免產生環(huán)路和重復計算,我們找到一個存在于字典的新的詞時,就要把它從字典中移去。代碼用來記錄跳數(shù)控制來確保一次循環(huán)只計算同一層的節(jié)點,有點像二叉樹遍歷循環(huán)這個詞從第一位字母到最后一位字母循環(huán)這一位被替換成個其他字母的情況

Word Ladder

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord,
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"] As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5.

Note: Return 0 if there is no such transformation sequence. All words have the same length. All words contain only lowercase alphabetic characters.

廣度優(yōu)先搜索 復雜度

時間 O(N) 空間 O(N)

思路

因為要求最短路徑,如果我們用深度優(yōu)先搜索的話必須遍歷所有的路徑才能確定哪個是最短的,而用廣度優(yōu)先搜索的話,一旦搜到目標就可以提前終止了,而且根據廣度優(yōu)先的性質,我們肯定是先通過較短的路徑搜到目標。另外,為了避免產生環(huán)路和重復計算,我們找到一個存在于字典的新的詞時,就要把它從字典中移去。這么做是因為根據廣度優(yōu)先,我們第一次發(fā)現(xiàn)詞A的路徑一定是從初始詞到詞A最短的路徑,對于其他可能再經過詞A的路徑,我們都沒有必要再計算了。

代碼
public class Solution {

    public int ladderLength(String beginWord, String endWord, Set wordDict) {
        Queue queue = new LinkedList();
        // step用來記錄跳數(shù)
        int step = 2;
        queue.offer(beginWord);
        while(!queue.isEmpty()){
            int size = queue.size();
            // 控制size來確保一次while循環(huán)只計算同一層的節(jié)點,有點像二叉樹level order遍歷
            for(int j = 0; j < size; j++){
               String currWord = queue.poll();
                // 循環(huán)這個詞從第一位字母到最后一位字母
                for(int i = 0; i < endWord.length(); i++){
                    // 循環(huán)這一位被替換成25個其他字母的情況
                    for(char letter = "a"; letter <= "z"; letter++){
                        StringBuilder newWord = new StringBuilder(currWord);
                        newWord.setCharAt(i, letter);
                        if(endWord.equals(newWord.toString())){
                            return step;    
                        } else if(wordDict.contains(newWord.toString())){
                            wordDict.remove(newWord.toString());
                            queue.offer(newWord.toString());
                        }
                    }
                } 
            }
            step++;
        }
        return 0;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/64458.html

相關文章

  • leetcode127. Word Ladder

    摘要:但是這種要遍歷所有的情況,哪怕是已經超過最小操作次數(shù)的情況,導致代碼超時。其實從另一個角度來說,這道題可以看做是廣度優(yōu)先算法的一個展示。按上文中的題目為例,可以將廣度優(yōu)先算法寫成以下形式。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find the length of short...

    Galence 評論0 收藏0
  • leetcode126. Word Ladder II

    摘要:題目要求相比于,要求返回所有的最短路徑。至于如何生成該有向圖,則需要通過廣度優(yōu)先算法,利用隊列來實現(xiàn)。將每一層的分別入棧。如果遇到則至該層結尾廣度優(yōu)先算法結束。通過這種方式來防止形成圈。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find all shortest transfo...

    cooxer 評論0 收藏0
  • [LeetCode/LintCode] Word Ladder

    摘要:使用,利用其按層次操作的性質,可以得到最優(yōu)解。這樣可以保證這一層被完全遍歷。每次循環(huán)取出的元素存為新的字符串。一旦找到和相同的字符串,就返回轉換序列長度操作層數(shù),即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...

    張金寶 評論0 收藏0
  • [LeetCode] 126. Word Ladder II

    摘要:存放過程中的所有集合為所有的結尾,則順序存放這個結尾對應的中的所有存放同一個循環(huán)的新加入的,在下一個循環(huán)再依次對其中元素進行進一步的把首個字符串放入新,再將放入,并將鍵值對放入,進行初始化 Problem Given two words (start and end), and a dictionary, find all shortest transformation sequenc...

    wayneli 評論0 收藏0
  • 127. Word Ladder

    摘要:題目解答主要解題思路的,把每一種可能的都放進去試,看能不能有一條線邊到代碼當然,這樣的時間還不是最優(yōu)化的,如果我們從兩頭掃,掃到中間任何一個能夠串聯(lián)起來都可以,如果沒有找到可以串聯(lián)的那么返回。 題目:Given two words (beginWord and endWord), and a dictionarys word list, find the length of short...

    forsigner 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<