[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 ...
随机推荐
- sql中的函数
函数类别 作用 聚合函数 执行的操作是将多个值合并为一个值.例如 COUNT.SUM.MIN 和 MAX. 配置函数 是一种标量函数,可返回有关配置设置的信息. 转换函数 将值从一种数据类型转换为另一 ...
- 尚硅谷springboot学习7-yaml配置文件
SpringBoot使用一个全局的配置文件,配置文件名是固定的: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值 ...
- 使用hibernate与mysql时数据不能插入的原因及解决办法
1.背景 之前从没用过hibernate,因此在网上搜了一下hibernate快速入门方面的信息,最后我按照<Myeclipse Hibernate 快速入门 中文版>(CSDN,百度文库 ...
- nginx的websock问题解决
生产环境中有一个项目需要使用到websock,但是项目上线后发现websock连接后马上断开,但是在测试环境没有问题,后来就想到配置文件和nginx版本问题 核对后发现,nginx和配置文件都是相同的 ...
- JAVA仿百度分页
最近在做一个仿百度网盘的网页小应用,找到了一个优雅简洁的分页插件,和百度搜索的分页很相似,对他进行了二次封装,拿出来跟大家分享下 插件源码 /** * This jQuery plugin displ ...
- python的垃圾回收机制【转】
http://python.jobbole.com/82061/ http://www.jianshu.com/p/1e375fb40506 https://www.cnblogs.com/vamei ...
- 闹钟AlarmAndMusic 和支持播放音乐效果《IT蓝豹》
闹钟AlarmAndMusic 和支持播放音乐效果的,上下滑动调整时间和页面旋转风车效果,由于制作的gif有些问题,效果不明显,欢迎下载使用看看真实的效果.本例子主要由AlertActivity和Al ...
- tabel 选中行变色和取当前选中行值等问题
先把代码贴出来 $("#tableId tbody tr").mousedown(function () { $('#tableId tr').each(funct ...
- hdu1042-N!-(java大数)
题目:求n!(0<=n<=10000) import java.math.BigInteger;//操作大整数 import java.math.BigDecimal;//操作大小数 im ...
- SVM参数解析
一.Opencv中的核函数定义(4种): 1.CvSVM::LINEAR : 线性内核,没有任何向映射至高维空间,线性区分(或回归)在原始特点空间中被完成,这是最快的选择. 2.CvSVM::POLY ...