[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 ...
随机推荐
- 尚硅谷springboot学习18-日志使用
默认配置 SpringBoot默认帮我们配置好了日志 //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test public v ...
- 模糊查询内存查询java实现
下面说说看到的工作项目中的代码,是这个样子的,事先查询一次数据库,将查询到的整张表的数据存到内存,以后使用时不再查询数据库,而直接操作内存中的数据,这主要用于数据库中的数据比较稳定,不会轻易改变的情况 ...
- python语言中的数据类型之元组
数据类型 元组 tuple 元组:不可变类型 用途:元组就是一个不可变的列表,当需要存不改动的值时可用元组 定义方式:在()内用逗号分隔开多个任意类型的元素 t=(1,2.2,'aa',( ...
- javascript中函数作用域和声明提前
javascript不像java等其他强类型语句,没有块级作用域(括号内的代码都有自己的作用域,变量在声明它们的代码段之外不可见)一说,但有自己的独特地方,即函数作用域. 函数作用域:变量在声明它们的 ...
- C语言函数入参压栈顺序为什么是从右向左?
看到有人提问到,在处理printf/cout时,压栈顺序是什么样的?大家都知道是从右往左,也就是说从右往左的计算,但是,这里的计算不等于输出. a++和++a的压栈的区别:在计算时,遇到a++会记录此 ...
- httpClient服务端编写
以前用过HttpClient,给你说几个关键的地方吧: 1. 首先,发送的时候 HttpClient client = new HttpClient(); PostMethod method = ne ...
- python 获取命令行参数
https://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html zzz.py import sys from optparse ...
- jq控制select值为某个时选中
$("#selectche_type option").each(function(){//用id ,用class好像不得,不知道为何 alert(2) if($(this).va ...
- 面向对象程序设计(Java)本学期课程改革说明
1.教学方式采用反转课堂教学法,学生课前完成知识的学习,课堂是师生之间.学生之间互动的场所,包括答疑解惑.知识的运用技巧探讨等,教师的课堂责任是理解学生的问题和引导学生去运用知识. 2.为更好地执行反 ...
- jquery使用post方法传值
1.js代码 <script type="text/javascript"> function addSku(skuId){ var m = $("#m&qu ...