单词接龙 java_[leetcode]127. Word Ladder单词接龙

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:

Only one letter can be changed at a time.

Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

Return 0 if there is no such transformation sequence.

All words have the same length.

All words contain only lowercase alphabetic characters.

You may assume no duplicates in the word list.

You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:

beginWord = "hit",

endWord = "cog",

wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",

return its length 5.

思路

BFS

代码

class Solution {

public int ladderLength(String beginWord, String endWord, List wordList) {

// use dict to check duplicats

Set dict = new HashSet<>(wordList);

Queue queue = new LinkedList<>();

queue.add(beginWord);

int level = 0;

while(!queue.isEmpty()){

int size = queue.size();

for(int i = 0; i < size; i++){

String cur = queue.remove();

if(cur.equals(endWord)){ return level + 1;}

for(int j = 0; j < cur.length(); j++){

// hit -> {'h', 'i', 't'}

char[] charArray = cur.toCharArray();

for(char c = 'a'; c <='z'; c++){

// {'h', 'i', 't'} for'h', try checking 'a','b'...'z' which forms ait, bit...zit

charArray[j] = c;

String temp = new String(charArray);

if(dict.contains(temp)){

queue.add(temp);

// to avoid dead loop, like hit will find hit itself

dict.remove(temp);

}

}

}

}

level++;

}

return 0;

}

}

LeetCode 127&period; Word Ladder 单词接龙&lpar;C&plus;&plus;&sol;Java&rpar;

题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

&lbrack;LeetCode&rsqb; 127&period; Word Ladder 单词阶梯

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

127 Word Ladder 单词接龙

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

leetcode 127&period; Word Ladder、126&period; Word Ladder II

127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

Leetcode&num;127 Word Ladder

原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

leetcode&commat; &lbrack;127&rsqb; Word Ladder &lpar;BFS &sol; Graph&rpar;

https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

leetcode 127&period; Word Ladder ----- java

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

&lbrack;LeetCode&rsqb; 127&period; Word Ladder &lowbar;Medium tag&colon; BFS

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

Java for LeetCode 127 Word Ladder

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

随机推荐

ASP&period;NET MVC开发中常见异常及解决方案

ASP.NET MVC4入门到精通系列目录汇总 NHibernate:no persister for 异常 1.配置文件后缀名写错 mapping file 必须是.hbm.xml结尾 2.Web. ...

C&num;获取年龄段 几零后

/// /// 根据年龄获得年龄段 /// ///

UML用例图总结

用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...

iOS 可延展视图(点击前显示部分文字,点击后显示全部)

#import @interface AppDelegate : UIResponder

javacript中的mvc设计模式

以下内容为原创翻译,翻译不对的地方还请原谅,凑合着看吧. 原文网址是: 来源:http://www.alexatnet.com/articles/model-view-controller-mvc-j ...

QT---系统托盘图标不显示原因

很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...

websphere安装验证报错 忘记密码的配置

http://blog.csdn.net/yulimin/article/details/4048897 ADMU7704E: 在尝试启动与服务器相关联的 Windows 服务时失败:server1: ...

C&num;匹配标签正则,获取标签的值

比如要获取: 头盔坐标:(-0.6, 1.0, 1.2)头盔方向(-0.2, 0.1, ...

mysql之Query Cache

1,QueryCache的实现原理: 1.目前只有select语句会被cache,其他类似show,use的语句则不会被cache. 2.两个SQL语句,只要相差哪怕是一个字符(例如大小写不一样:多一 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值