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

给定两个单词(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;
    }

【1】【leetcode-127】单词接龙word-ladder的更多相关文章

  1. Java实现 LeetCode 127 单词接龙

    127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...

  2. [Swift]LeetCode127. 单词接龙 | Word Ladder

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. leetcode 127 单词接龙

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...

  4. Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)

    Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. Java实现 LeetCode 126 单词接龙 II

    126. 单词接龙 II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: ...

  7. Leetcode 126.单词接龙II

    单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...

  8. [LeetCode] 126. 单词接龙 II

    题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...

  9. 127单词接龙 1· Word Ladder1

    找出最短路径 [抄题]: Given two words (beginWord and endWord), and a dictionary's word list, find the length ...

  10. LeetCode 79. 单词搜索(Word Search)

    题目描述 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被 ...

随机推荐

  1. Jdk1.6编译,1.7执行,1.7中没有需要的类,为何不会报错

      今天发现一个非常奇怪的问题   import sun.misc.BASE64Decoder; 我在本地引入了jdk1.6中的包,编译也用1.6没问题,但是实际上jdk1.7并没有这个包.在Linu ...

  2. MT【289】含参绝对值的最大值之三

    已知$a>0$,函数$f(x)=e^x+3ax^2-2e x-a+1$,(1)若$f(x)$在$[0,1]$上单调递减,求$a$的取值范围.(2)$|f(x)|\le1$对任意$x\in[0,1 ...

  3. 用webpack2.0构建vue2.0单文件组件超级详细精简实例

    npm init -y 初始化项目  //-y 为自动生成package.json,如果需要自行配置,去掉-y即可 安装各种依赖项 npm install --save vue 安装vue2.0 np ...

  4. 【转】STM32: 一种计算CPU使用率的方法及其实现原理

    1  前言出于性能方面的考虑,有的时候,我们希望知道CPU的使用率为多少,进而判断此CPU的负载情况和对于当前运行环境是否足够“胜任”.本文将介绍一种计算CPU占有率的方法以及其实现原理. 2  移植 ...

  5. for循环是怎么工作的

    for...in 是Python程序员使用最多的语句,for 循环用于迭代容器对象中的元素,这些对象可以是列表.元组.字典.集合.文件,甚至可以是自定义类或者函数,例如: 作用于列表 >> ...

  6. VSIX 插件右键菜单

    vs2017 插件开发 环境 WIN10 VS2017 CMMT VSIX 参考资源: vs菜单命令ID速查 https://docs.microsoft.com/zh-cn/visualstudio ...

  7. js中的变量提升与函数提升

    先看看一个简单的代码 var str='Hello World'; alert(str);//弹出 Hello World 再看一段代码: var v='Hello World'; (function ...

  8. our happy ending(状压dp)

    题意:给定一个n,k,l. 问有多少长度为n的序列满足选出一些数使得他们相加为k,数列中每个数都在1-l以内. Solution 正解还是很妙的. 状压dp,设dp[i][j]表示长度为i的序列,能表 ...

  9. poj1664放苹果(递归)

    题目链接:http://poj.org/problem?id=1664 放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  10. js 触摸的Event--获取触摸位置

    继上一篇js原生拖拽之后,现在又来写一下移动端touch列表,获取触摸位置.pc端的event事件,鼠标的位置信息在上一篇,点此进入上一篇. touch有3个事件:touchstart,touchmo ...