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 ...
随机推荐
- [SVN] 分支同步、合入主干操作分享
冲突的解决原则 不是自己修改的地方就使用主干的. 需要特别注意的是: 分支同步主干时,远端(theirs)是主干,本地(mine/working)的是分支: 分支合入主干时,本地(mine/worki ...
- iPhone App开发实战手册学习笔记(9)之设计IOS App的目标
1 前言 如果我们要做一个属于自己的App需要达到那些目标呢,今天就来介绍一下. 2 详述 2.1 关注用户及其需求 你的主要目标永远都是在设计方案之前先想好用户用例.有些开发人员喜欢编写用户故事来确 ...
- sublime搜索和替换--正则
Search and Replace Sublime Text features two main types of search: Search - Single File Search - Mul ...
- HDU 4974 A simple water problem(贪心)
HDU 4974 A simple water problem pid=4974" target="_blank" style="">题目链接 ...
- qt槽函数中,窗口镶嵌窗口的问题,求解
my_label=newQLabel(ui->widget); my_Label->setText("yvhvv"); 我把这插入到构造函数中,正确显示. 我把这插入到 ...
- <ctype.h> C语言标准库
ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于測试字符是否属于特定的字符类别.如字母字符.控制字 ...
- Label的各个属性
- TWinControl的消息覆盖函数大全(41个WM_函数和31个CM_函数,它的WndProc就处理鼠标(转发)、键盘(取消拖动)、焦点、和WM_NCHITTEST一共4类消息)
注意,这些函数只有Private一种形式(也就是不允许覆盖,但仍在动态表格中): 其中TWinControl对TControl有10个消息进行了覆盖(红色标记),其中有2个是WM_消息,8个是CM_消 ...
- Delaunay三角剖分算法
在图像处理中,经常会使用到三角剖分算法: 具体定义及其算法可以参考:http://baike.so.com/doc/5447649.html 下面放出来代码: Delaunay接口为存C: 测试是使用 ...
- lua的table库中经常使用的函数
lua提供了一些辅助函数来操作table. 比如,从list中insert和remove元素,对array的元素进行sort.或者concatenate数组中的全部strings.以下就具体地解说这些 ...