leetcode 127. Word Ladder ----- java

时间:2021-11-04 04:54:35

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. &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 ...

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

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

  3. 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 ...

  4. Leetcode&num;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&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 ...

  7. &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 ...

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

  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. rotate the clock

    A program test: You are given N round clocks. Every clock has M hands, and these hands can point to ...

  2. RAD DELPHI XE5的android开发环境配置

    RAD XE5 支持本地化跨平台编译(IOS,OS-X,WIN 64,WIN32,ANDROID) 对于android的开发环境,XE5支持模拟器,和真机设备两种模式: 1. 模拟器:(支持4.0.3 ...

  3. xcode 上 crash 调试的三种方法

    最近有新人问crash调试方法,简介记录如下: 模拟器调试 打开控制台查看输出日志 显示出错的行数 显示出错的函数iOS Crash跟踪 真机调试 首先修改真机调试的 bundle ID,使代码可以进 ...

  4. 【转】IOS7 MPMoviePlayerViewController横屏显示

    在应用程序中用到MPMoviePlayerViewController时,有时需要保持应用程序为竖屏状态,而视频播放器显示为横屏,如何做呢?如果采用强制横屏的方法,应用审核的时候是不会通过的,因为该方 ...

  5. (转)C&sol;C&plus;&plus;中static关键字

    下面的转自http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777441.html 静态变量作用范围在一个文件内,程序开始时分配空间,结束 ...

  6. HDU4046--Panda&lpar;树状数组&rpar;

    Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. 2018-2019-20175205 实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告

    2018-2019-20175205 实验三敏捷开发与XP实践<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...

  8. python jieba 库分词结合Wordcloud词云统计

    import jieba jieba.add_word("福军") jieba.add_word("少安") excludes={"一个", ...

  9. &lbrack;Android Pro&rsqb; AndroidStudio IDE界面插件开发(进阶篇之Action机制)

    转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53883500] 从上一篇<AndroidSt ...

  10. js代码中碰到的函数

    第一个--->字符串的截取substring()方法 substring(a,b)--->[a,b)区间截取字符.下标从0开始.从a下标开始,截取到b下标的前一个字符.返回一个新的字符串 ...