• 【1】【leetcode-127】单词接龙word-ladder


     (不会,经典广度优先搜索

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:

    1. 每次转换只能改变一个字母。
    2. 转换过程中的中间单词必须是字典中的单词。

    说明:

    • 如果不存在这样的转换序列,返回 0。
    • 所有单词具有相同的长度。
    • 所有单词只由小写字母组成。
    • 字典中不存在重复的单词。
    • 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

    示例 1:

    输入:
    beginWord = "hit",
    endWord = "cog",
    wordList = ["hot","dot","dog","lot","log","cog"]
    
    输出: 5
    
    解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
         返回它的长度 5。
    

    示例 2:

    输入:
    beginWord = "hit"
    endWord = "cog"
    wordList = ["hot","dot","dog","lot","log"]
    
    输出: 0
    
    解释: endWord "cog" 不在字典中,所以无法进行转换。



    关键:
    1.广度优先搜索->队列
    2.判断层数要熟练,不要每次都想
            while(!queue.isEmpty()) {//固定的层数记录形式
                layer++;
                int size = queue.size();
                while (size-->0) {


    参考的解法:https://leetcode-cn.com/problems/word-ladder/comments/
    class Solution {
        //BFS的思想
       public int ladderLength(String beginWord, String endWord, List<String> wordList) {
            Queue<String> queue = new LinkedList<String>();//少不了队列
            queue.add(beginWord);
            boolean[] marked = new boolean[wordList.size()+1];//少不了标记
            int layer = 1;//注意返回的是层数+1.所以这里直接放1了
            while(!queue.isEmpty()) {//固定的层数记录形式
                layer++;
                int size = queue.size();
                while (size-->0) {
                    String cur = queue.poll();
                    for (int i = 0; i < wordList.size(); i++) {
                        if(marked[i])continue;
                        String dic = wordList.get(i);
                        if(canChange(dic, cur)) {
                            if(dic.equals(endWord))return layer;
                            queue.add(dic);
                            marked[i] = true;
                        }
                    }
                }
            }
            
            return 0;
        }
        //是否可以转换的辅助函数
        public boolean canChange(String s,String t) {
            int len = s.length();
            int diff = 0;
            for (int i = 0; i < s.length(); i++) {
                if(s.charAt(i) != t.charAt(i))diff++;
            }
            return diff==1;
        }
    }
    牛客的题是HashSet<String>,注意不用Iterator遍历会报ConcurrentModificationException

    链接:https://www.nowcoder.com/questionTerminal/bd75ae43ff7148548eb4701550df2714
    来源:牛客网

    public int ladderLength(String start, String end, HashSet<String> dict) {  
          int res=1;
          LinkedList<String> queue=new LinkedList<>();
          queue.offer(start);
          while(!queue.isEmpty())
          {
              int size=queue.size();
              while(size>0)
              {
                  String s=queue.poll();
                  size--;
                   
                  if(isDiffOne(s, end))
                      return res+1;
                  for(Iterator<String> it=dict.iterator();it.hasNext();)
                  {
                      String str=it.next();
                      if(isDiffOne(str, s))
                      {
                          queue.offer(str);
                          it.remove();
                      }
                           
                  }
              }
              res++;
          }
          return 0;
        }  
       
        public boolean isDiffOne(String w1, String w2) {  
           int count = 0;          
           for(int i = 0; i < w1.length(); i++) {  
               if(w1.charAt(i) != w2.charAt(i)) {  
                   count++;  
               }  
           }
           return count==1?true:false;
        }
  • 相关阅读:
    LeetCode Product of Array Except Self
    python基础学习笔记(十)
    python基础学习笔记(九)
    python基础学习笔记(八)
    python基础学习笔记(六)
    python基础学习笔记(七)
    python基础学习笔记(五)
    python基础学习笔记(一)
    python基础学习笔记(三)
    python基础学习笔记(四)
  • 原文地址:https://www.cnblogs.com/twoheads/p/10577651.html
Copyright © 2020-2023  润新知