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. Linux-Gcc生成和使用静态库和动态库详解

    一.基本概念 1.1什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行. 由于windows和linux的平台不同( ...

  2. 房子里的K2 BPM业务流程管理

    房…子这件事上,尴尬实在太多. ಥ_ಥ 职场新人,租房很尴尬: 未婚男女,婚房很尴尬: 有下一代的,学区房很尴尬: 耄耋之年,养老房很尴尬... ▽ 甭管买房.租房.装修.设计,关于房子的尴尬事,三天 ...

  3. SrcollView分页加载数据(MainActivity)

    package com.baidu.mylistscroll; import java.util.ArrayList;import java.util.List; import com.baidu.a ...

  4. Struts2 validate校验

    一般的,用户注册的时候,我们需要校验一些用户提交过来的参数. 一般有两道屏障,一是在前台页面上使用js进行验证,直接杜绝了不正常信息的提交.二是将提交过来的信息进行验证,不通过则返回注册页面并显示错误 ...

  5. VMWare三种工作模式 :bridge、host-only、nat

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式.这里 ...

  6. Date的转换

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String str = sdf.format(一个date对 ...

  7. JS设计模式书籍、原则

    圣经书:JavaScript 高级程序设计 设计模式:DesignPatterns by Erich Gamma.Richard Hlem.Ralph Johnson .Jhon Vlissides ...

  8. Apache虚拟主机(三)

    一.启用 httpd-vhosts.conf 在httpd.conf文件中启用 在文件中搜索:Virtual hosts #Virtual hosts虚拟主机 Include conf/extra/h ...

  9. SyntaxError: missing ; before statement 错误的解决

    今天jsp页面中报错:SyntaxError: missing ; before statement 简单的理解是语法错误,F12调试之后发现原来是我定义的一个js中的全局变量的问题. <scr ...

  10. NSURLSession概述

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...