My Solution: Word Ladder
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 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的更多相关文章
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
随机推荐
- v$lock 视图访问慢解决方法
V$ 视图访问慢 --解决方法 分析:可能是有数据字典统计信息过久,造成. exec dbms_stats.gather_fixed_objects_stats; ------收集所有数据字典的fix ...
- ORACLE 五种表的优缺点总结
ORACLE 五种表的优缺点总结: 1.普通表(heap table):适合大部分设计场景,有长处也有缺点. 长处: a,语法简单方便 b,适合大部分场景 缺点: a,更新日志开销较大 b,Delet ...
- 什么是防盗链设置中的空Referer
设置防盗链时候指明和不指明空Referer的差别及实现后的效果? 什么是Referer? 这里的 Referer 指的是HTTP头部的一个字段,也称为HTTP来源地址(HTTP Referer).用来 ...
- 运行yum报错Error: Cannot retrieve metalink for reposit
http://www.netpc.com.cn/593.html 运行yum报错Error: Cannot retrieve metalink for reposit 今天给Centos通过rpm - ...
- Android中ProgressDialog的应用
下面通过实现点击按钮来显示加载框,2秒后自动消失. 1.首先在layout的xml中添加一个按钮: <Button android:id="@+id/button1" and ...
- java面向对象下:Java数据库编程
19.Java数据库编程: JDBC概述: JDBC(Java Database Connection)是java中提供的一套数据库编程API,它定义了一套用来访问数据库的标准Java类 ...
- 代码写解压zip文件
最近项目中服务器方返回了zip文件类型的文件,在网上搜了好多资料做成一个Demo,这里用来详解一下. ZipArchive类来源于网络.还望多多交流. 1.首先添加libz.dylib框架 2.前往h ...
- Effective C++ 24,25
24.在函数重载和设定參数缺省值间要谨慎选择. 获得一种类型的数据的最小值或最大值,对于c中,一般使用在<linits.h>中定义的各种宏如INT_MIN 来进行表示,可是这样无法进行泛型 ...
- rsync使用指南
考虑到服务器数据的安全,我考虑增加一台备份服务器,通过数据同步,达到较好的冗余. linux下有非常好的一个命令rsync可以实现差异备份,下面就说说它的用法:ubuntu缺省安装的安装中,rsync ...
- 14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新:
14.4.3.6 Fine-tuning InnoDB Buffer Pool Flushing 微调 InnoDB Buffer Pool 刷新: innodb_flush_neighbors an ...