鱼C论坛

 找回密码
 立即注册
查看: 772|回复: 0

[学习笔记] leetcode 127. Word ladder

[复制链接]
发表于 2019-12-24 11:22:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 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:

  2. Only one letter can be changed at a time.
  3. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
  4. Note:

  5. Return 0 if there is no such transformation sequence.
  6. All words have the same length.
  7. All words contain only lowercase alphabetic characters.
  8. You may assume no duplicates in the word list.
  9. You may assume beginWord and endWord are non-empty and are not the same.
  10. Example 1:

  11. Input:
  12. beginWord = "hit",
  13. endWord = "cog",
  14. wordList = ["hot","dot","dog","lot","log","cog"]

  15. Output: 5

  16. Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
  17. return its length 5.
  18. Example 2:

  19. Input:
  20. beginWord = "hit"
  21. endWord = "cog"
  22. wordList = ["hot","dot","dog","lot","log"]

  23. Output: 0

  24. Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
复制代码



BFS

  1. class Solution {
  2.     public int ladderLength(String beginWord, String endWord, List<String> wordAsList) {
  3.         if(!wordAsList.contains(endWord)){
  4.             return 0;
  5.         }
  6.         
  7.         Queue<String> beginSet = new LinkedList<String>();
  8.         HashSet<String> wordList = new HashSet<String>(wordAsList);
  9.         
  10.         beginSet.add(beginWord);
  11.         int level = 0;
  12.         
  13.         while(!beginSet.isEmpty()){
  14.             
  15.             int size = beginSet.size();
  16.             for(int i = 0 ; i< size; i++){
  17.                String word = beginSet.poll();
  18.                 if(word.equals(endWord)) return level+1;
  19.                 char[] wordset = word.toCharArray();
  20.                 for(int k = 0 ; k< wordset.length; k++){
  21.                     char temp = wordset[k];

  22.                     for(char j = 'a' ; j<= 'z'; j++){
  23.                         wordset[k] = j;
  24.                         String str = new String(wordset);

  25.                         if(wordList.contains(str)){
  26.                             wordList.remove(str);
  27.                             beginSet.add(str);
  28.                         }
  29.                     }

  30.                     wordset[k] = temp;
  31.                 }
  32.             }
  33.             
  34.             level ++;
  35.         }
  36.         
  37.         return 0;
  38.     }
  39. }
复制代码


Bidirection BFS

  1. class Solution {
  2.     public int ladderLength(String beginWord, String endWord, List<String> wordAsList) {
  3.         if(!wordAsList.contains(endWord)){
  4.             return 0;
  5.         }
  6.         
  7.         Set<String> beginSet = new HashSet<String>();
  8.         Set<String> endSet = new HashSet<String>();
  9.         
  10.         HashSet<String> wordList = new HashSet<String>(wordAsList);
  11.         
  12.         beginSet.add(beginWord);
  13.         endSet.add(endWord);
  14.         int level = 1;
  15.         
  16.         while(!beginSet.isEmpty() && !endSet.isEmpty()){
  17.             if(beginSet.size() > endSet.size()){
  18.                 Set <String> temp = endSet;
  19.                 endSet = beginSet;
  20.                 beginSet = temp;
  21.             }
  22.             
  23.             Set <String> temp = new HashSet<String>();
  24.             
  25.                
  26.                for(String word : beginSet){
  27.                     
  28.                     char[] wordset = word.toCharArray();
  29.                     int len = beginWord.length();
  30.                     for(int k = 0 ; k< len; k++){
  31.                     char temp1 = wordset[k];

  32.                     for(char j = 'a' ; j<= 'z'; j++){
  33.                         wordset[k] = j;
  34.                         String str = new String(wordset);
  35.                         
  36.                         if(endSet.contains(str)) return level+1;
  37.                         
  38.                         if(!wordList.contains(str)) continue;

  39.                         
  40.                             wordList.remove(str);
  41.                             temp.add(str);
  42.                         
  43.                     }

  44.                     wordset[k] = temp1;
  45.                     
  46.                }
  47.             }
  48.             Set <String> temp2 = beginSet;
  49.             beginSet = temp;
  50.             temp = temp2;
  51.             level ++;
  52.         }
  53.         
  54.         return 0;
  55.     }
  56. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 02:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表