leetcode 127. Word Ladder ----- java

來源:互聯網
上載者:User

標籤:media   去掉m   begin   int   []   list   empty   tin   hot   

Given two words (beginWord and endWord), and a dictionary‘s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

由於現做的126題,所以這道題其實只用BFS就可以了。

用126的答案。

public class Solution {    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {        if( beginWord == null || beginWord.length()  == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )            return 0;        return BFS(beginWord,endWord,wordList);    }    public int BFS(String beginWord,String endWord,Set<String> wordList){        Queue queue = new LinkedList<String>();        queue.add(beginWord);        int result = 1;        while( !queue.isEmpty() ){            String str = (String) queue.poll();            if( str.equals(endWord) )                continue;            for( int i = 0 ;i <beginWord.length();i++){                char[] word = str.toCharArray();                for( char ch = ‘a‘;ch<=‘z‘;ch++) {                    word[i] = ch;                    String Nword = new String(word);                    if ( wordList.contains(Nword)) {                        if (!map.containsKey(Nword)) {                            map.put(Nword, (int) map.get(str) + 1);                            queue.add(Nword);                        }                    }                    if(  Nword.equals(endWord)  )                        return (int) map.get(str) + 1;                }            }        }        return 0;    }}

 

 

去掉map,會快一些。

public class Solution {    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {        if( beginWord == null || beginWord.length()  == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )            return 0;        Queue queue = new LinkedList<String>();        queue.add(beginWord);        int result = 1;        while( ! queue.isEmpty() ){            int len = queue.size();            for( int i = 0;i<len;i++){                String str = (String) queue.poll();                for( int ii = 0; ii < str.length();ii++){                    char[] word = str.toCharArray();                    for( char ch = ‘a‘; ch<=‘z‘;ch++){                        word[ii] = ch;                        String newWord = new String(word);                        if( wordList.contains(newWord) ){                            wordList.remove(newWord);                            queue.add(newWord);                        }                        if( newWord.equals(endWord) )                            return result+1;                    }                }            }            result++;        }        return 0;    }}

還有更快的做法,一般是前後一起建立隊列來做,會快很多。

leetcode 127. Word Ladder ----- java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.