public class Solution {
public int ladderLength(String start, String end, Set<String> dict) {
if (start == null || end == null || dict == null
|| start.length() != end.length()) {
return 0;
}
if (oneDiff(start, end)) { return 2;
}
return helper(start, end, dict, 2); } public boolean oneDiff(String left, String right) {
int sameNumber = 0;
if (left == null || right == null) {
return false;
}
if (left.length() != right.length()) {
return false;
}
for (int i = 0; i < left.length(); i++) {
if (left.charAt(i) == right.charAt(i)) {
sameNumber++;
}
}
if (sameNumber == left.length() - 1) {
return true;
}
return false;
} public int helper(String start, String end, Set<String> dict, int level) {
Queue<String> queue = new LinkedList<String>();
queue.offer(start);
HashMap<String,Integer> route = new HashMap<String,Integer>();
route.put(start,0);
while (!queue.isEmpty()) {
int qSize = queue.size();
for (int i = 0; i < qSize; i++) {
String cur = queue.poll();
for (int j = 0; j < cur.length(); j++) {
for (char c = 'a'; c <= 'z'; c++) {
if (cur.charAt(j) == c) {
continue;
}
StringBuffer sb = new StringBuffer(cur);
sb.setCharAt(j, c);
String nowStr = sb.toString(); if (nowStr.equals(end)) {
return level++;
}
if (dict.contains(nowStr)
&& !route.containsKey(nowStr)) {
queue.offer(nowStr);
route.put(nowStr,0); }
}
} }
level++;
}
return 0;
}
}


之后发现这个太丑了,用了算法导论上的方法,找一个东西来记录层数:
public class Solution {
public int ladderLength(String start, String end, Set<String> dict) {
if (start == null || end == null || dict == null
|| start.length() != end.length()) {
return 0;
} return helper(start, end, dict, 1); } public int helper(String start, String end, Set<String> dict, int level) {
Queue<String> queue = new LinkedList<String>();
queue.offer(start);
HashMap<String, Integer> route = new HashMap<String, Integer>();
route.put(start, 1);
while (!queue.isEmpty()) {
String cur = queue.poll();
int curLevel = route.get(cur); for (int j = 0; j < cur.length(); j++) {
for (char c = 'a'; c <= 'z'; c++) {
if (cur.charAt(j) == c) {
continue;
}
StringBuffer sb = new StringBuffer(cur);
sb.setCharAt(j, c);
String nowStr = sb.toString(); if (nowStr.equals(end)) {
return ++curLevel;
}
if (dict.contains(nowStr) && !route.containsKey(nowStr)) {
queue.offer(nowStr);
route.put(nowStr, curLevel + 1); }
}
} }
return 0; }
}

My Solution: Word Ladder的更多相关文章

  1. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  2. [LeetCode] Word Ladder 词语阶梯

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

  3. [LeetCode] Word Ladder II 词语阶梯之二

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

  4. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  5. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  6. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. LeetCode127:Word Ladder II

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

  8. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  9. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

随机推荐

  1. eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“

    你的位置:首页 > Java编程 > eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“ eclipse发布项目报错:Multipl ...

  2. Hadoop--序列化

    序列化: 对象的序列化用于将一个对象编码成字节流,以及从字节流中重新构建对象. 将一个对象编码成一个字节流称为序列化该对象. 序列化三种主要的用途: 1.作为一种持久化格式. 2.作为一种通信的数据格 ...

  3. Swift - 继承UIView实现自定义可视化组件(附记分牌样例)

    在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现.下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举.协议等相关知识的学习. 效果图如下:    组件代码:Score ...

  4. 浙江大学PAT上机题解析之2-06. 数列求和

    给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + … + AA…A(N个A).例如A=1, N=3时,S ...

  5. [UVALive 6663 Count the Regions] (dfs + 离散化)

    链接:https://icpcarchive.ecs.baylor.edu/index.php? option=com_onlinejudge&Itemid=8&page=show_p ...

  6. ExtJs4 笔记(6) Ext.MessageBox 消息对话框

    本篇演示消息对话框的用法,ExtJs封装了可能用到的各类消息框,并支持自定义的配置. 如下是用到的html: [html] <h1>各种消息框</h1> <div id= ...

  7. Linux 多线程串口通信

    大概流程就是打开一个串口.然后进行串口设置.开启二个线程,一个线程写数据,另一个线程读数据. 代码如下: #include <stdio.h> #include <stdlib.h& ...

  8. Android 编程之第三方开发 MaoZhuaWeiBo微博开发演示样例-1

    在大学期间我做过非常多类似这种APP.这个是我们小组之前做的,我后期增加非常多新元素.完好了这个应用,由于为了加强 专业技术嘛.也是常常熬夜写些小东西,嘿嘿.只是还算不错.起码技术长进了不少嘛,还是非 ...

  9. 使用Android简单实现有道电子词典

    前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...

  10. VC生成的DLL给QT的EXE调用时lib路径问题小结

    VC生成的DLL给QT调用,有两种方式,一种是隐式调用调用(使用.lib文件方式): ① 在*.pro工程文件中添加VC生成的lib文件路径时,或者使用一个绝对路径,如: LIBS += " ...