[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<String> wordList) {
// use dict to check duplicats
Set<String> dict = new HashSet<>(wordList);
Queue<String> 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. Word Ladder单词接龙的更多相关文章
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- [LeetCode] 127. 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. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 127. Word Ladder _Medium tag: 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 ...
随机推荐
- Linux命令:ssh-copy-id
ssh-copy-id帮助 SSH-COPY-ID() BSD General Commands Manual SSH-COPY-ID() NAME ssh-copy-id — use locally ...
- Redis进阶实践之一VMWare Pro虚拟机安装和Linux系统的安装(转载)(1)
Redis进阶实践之一VMWare Pro虚拟机安装和Linux系统的安装 一.引言 设计模式写完了,相当于重新学了一遍,每次学习都会有不同的感受,对设计模式的理解又加深了,理解的更加透彻了.还差一篇 ...
- Python基本模块介绍:sys、platform
sys模块 常用函数 sys.argv 命令行参数,实现从程序外部向程序传递参数. sys.path 模块搜索路径. sys.platform 获取当前系统平台. sys.version 获取pyth ...
- JSTL如何遍历Servlet传过来的list和map,用例子说明
后端 List<Article> list = dao.getPageList(nameid,Integer.parseInt(page)); request. ...
- Android 设备,如何root,执行adb shell,查看设备中的数据库信息等【转】
原文地址: Android 设备,如何root,执行adb shell,查看设备中的数据库信息等
- SQL Server--存在则更新问题
在博客园看到一篇讨论特别多的文章“探讨SQL Server并发处理存在就更新七种解决方案”,这种业务需求很常见:如果记录存在就更新,不存在就插入. 最常见的做法: BEGIN TRANSACTION ...
- Eclipse绿豆沙护眼
Window-->Preferences-->Editors——>Text Editors —— Background color 背景颜色向你推荐:色调:85.饱和度:123.亮度 ...
- 吴裕雄 06-MySQL选择数据库
实例以下实例选取了数据库 RUNOOB:use RUNOOB;注意:所有的数据库名,表名,表字段都是区分大小写的.所以你在使用SQL命令时需要输入正确的名称. 使用PHP脚本选择MySQL数据库PHP ...
- RMI 、RPC和SOAP
- java 基础最全网站
http://www.runoob.com/java/java-tutorial.html