• leetcode 127. Word Ladder ----- java


    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;
        }
    }

    还有更快的做法,一般是前后一起建立队列来做,会快很多。

  • 相关阅读:
    websocket初体验(能传文字和图片)
    展开折叠效果 height未知 transition无效
    微信小程序自定义键盘
    微信小程序 selectComponent 值为null
    css 斜线 animation
    【转】怎样在ubuntu12.04下创建一个启动器
    以ontouch为例说明android事件发送机制
    谈谈移动应用设计——从一个普通开发者的角度
    Launch error: Failed to connect to remote VM. Connection refused.的解决办法
    Beyond compare代码比较工具。
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6035912.html
Copyright © 2020-2023  润新知