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:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.
    • All words contain only lowercase alphabetic characters.

由于现做的126题,所以这道题其实只用BFS就可以了。

用126的答案。

public class Solution {

    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
if( beginWord == null || beginWord.length() == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )
return 0;
return BFS(beginWord,endWord,wordList);
} public int BFS(String beginWord,String endWord,Set<String> wordList){ Queue queue = new LinkedList<String>();
queue.add(beginWord);
int result = 1;
while( !queue.isEmpty() ){
String str = (String) queue.poll();
if( str.equals(endWord) )
continue;
for( int i = 0 ;i <beginWord.length();i++){
char[] word = str.toCharArray();
for( char ch = 'a';ch<='z';ch++) {
word[i] = ch;
String Nword = new String(word);
if ( wordList.contains(Nword)) {
if (!map.containsKey(Nword)) {
map.put(Nword, (int) map.get(str) + 1);
queue.add(Nword);
}
}
if( Nword.equals(endWord) )
return (int) map.get(str) + 1;
}
}
}
return 0;
}
}

去掉map,会快一些。

public class Solution {

    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
if( beginWord == null || beginWord.length() == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )
return 0; Queue queue = new LinkedList<String>();
queue.add(beginWord);
int result = 1;
while( ! queue.isEmpty() ){
int len = queue.size();
for( int i = 0;i<len;i++){
String str = (String) queue.poll();
for( int ii = 0; ii < str.length();ii++){
char[] word = str.toCharArray();
for( char ch = 'a'; ch<='z';ch++){
word[ii] = ch;
String newWord = new String(word);
if( wordList.contains(newWord) ){
wordList.remove(newWord);
queue.add(newWord);
}
if( newWord.equals(endWord) )
return result+1;
}
}
}
result++;
}
return 0;
}
}

还有更快的做法,一般是前后一起建立队列来做,会快很多。

leetcode 127. Word Ladder ----- java的更多相关文章

  1. [LeetCode] 127. Word Ladder 单词阶梯

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

  2. leetcode 127. Word Ladder、126. Word Ladder II

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

  3. LeetCode 127. Word Ladder 单词接龙(C++/Java)

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

  4. Leetcode#127 Word Ladder

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

  5. Java for LeetCode 127 Word Ladder

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

  6. leetcode@ [127] Word Ladder (BFS / Graph)

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

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

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

  8. [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 ...

  9. Java for LeetCode 126 Word Ladder II 【HARD】

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

随机推荐

  1. MATLAB 函数

    MATLAB函数大全 1.常见 http://wenku.baidu.com/link?url=tPpwD7Ox_1sG-SQv_XdYszBAPY9LX_Zb_dde_5JeOiu7RwN_i14X ...

  2. UITextView的字数限制 及 添加自定义PlaceHolder

    - (void)textViewDidChange:(UITextView *)textView{ NSString *temp=textView.text; //字数超过限制数量时,进行截取替换 i ...

  3. Java基础毕向东day05 对象与对象的区别,匿名内部类,函数的执行流程。

    1.Car c = new Car(); Car c2 = new Car(); 1> c 和 c2之间的区别? public static void main(String[] args) { ...

  4. Oracle普通索引,唯一索引,主键的区别

    索引是我们经常使用的一种数据库优化手段,适当的业务操作场景使用适当的索引方案,可以显著的提升系统整体查询性能,当然用户体验也随之提高. 在Oracle中,唯一性索引(Unique Index)是我们经 ...

  5. javascript 变量声明有var与无var 的区别

    1.在函数作用域内 加var定义的变量是局部变量,不加var定义的就成了全局变量.使用var定义var a = 'hello World';function bb(){var a = 'hello B ...

  6. SwipeRefreshLayout

    也许之前下拉刷新你可能会用到一些第三方开源库,如PullToRefresh, ActionBar-PullToRefresh.XlistView等 但现在已经有官方的组件了---SwipeRefres ...

  7. hdu 2075

    PS:水得不能再水..刚开始还以为是大数..要用到快速幂...谁知道想太多...就普通int型.. 代码: #include "stdio.h" int main(){ int a ...

  8. 封装定制的Kali Live ISO

    打造专属的Kali ISO – 简介 封装定制的Kali ISO很简单,很有趣,很有意义.你可以用Debian的live-build脚本对Kali ISO进行全面的配置.这些脚本以一系列配置文件的方式 ...

  9. 显示ios设备信息的程序

    以下是运行在本人iphone4上的截图,支持中文简体,中文繁体,英文,支持iphone和ipad,当然由于没有ipad,ipad的测试用的模拟器.支持iphone4的Retina屏幕.本来有6个标签, ...

  10. 《View Programming Guide for iOS》之frame、bounds和center之间的关系

    The frame property contains the frame rectangle, which specifies the size and location of the view i ...