[Leetcode]Word Ladder
来源:互联网 发布:淘宝网店海报设计技巧 编辑:程序博客网 时间:2024/06/05 16:35
[题目]
Given two words (beginWord and endWord), and a dictionary, find the length ofshortest transformation sequence frombeginWord to endWord, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["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.
[思路]
思路是BFS。。。。
一个词语变动一个字母,有很多种情况:26 * word.length() ,都要遍历到,而且dictionary里面可能有不止一个候选,所以每一层,要用一个queue来存储。
string.subString(int start, int end):只能取到start - > end -1
- start the offset of the first character
- end the offset one past the last character
[代码]
并没有通过测试,理由是:out of time limited..
public int ladderLength(String start, String end, Set<String> dict) { LinkedList<String> queue = new LinkedList<String>(); queue.add(start); dict.add(end); int step = 0; while (!queue.isEmpty()) { LinkedList<String> level = new LinkedList<String>(); step++; while (!queue.isEmpty()) { String q = queue.pollFirst(); if (q.equals(end)) return step; for (int i = 0; i < start.length(); i++) { for (char c = 'a'; c <= 'z'; c++) { String s = q.substring(0, i) + c + q.substring(i + 1, start.length()); if (dict.contains(s)) { level.add(s); dict.remove(s); } } } } queue = level; } return 0;}
唯一的区别就是将string q没有直接使用substring, 而是换成了charArray(),然后替换r里面第i个,在把新组成的string s 放在dict里面判断。
public int ladderLength(String start, String end, Set<String> dict) { LinkedList<String> queue = new LinkedList<String>(); queue.add(start); dict.add(end); int step = 0; while (!queue.isEmpty()) { LinkedList<String> level = new LinkedList<String>(); step++; while (!queue.isEmpty()) { String q = queue.poll(); if(q.equals(end)) return step; char[] t = q.toCharArray(); for(int i = 0; i < start.length(); i++){ for(char c = 'a'; c <= 'z'; c++){ char temp = t[i]; t[i] = c; String s = String.copyValueOf(t); t[i] = temp; if(dict.contains(s)){ level.add(s); dict.remove(s); } } } } queue = level; } return 0;}
0 0
- [Leetcode][python]Word Ladder/Word Ladder II
- [Leetcode] Word Ladder
- LeetCode: Word Ladder
- leetcode 84: Word Ladder
- [leetcode] word ladder
- 【leetcode】Word Ladder
- 【leetcode】Word Ladder II
- LeetCode - Word Ladder
- LeetCode之Word Ladder
- [LeetCode]Word Ladder
- [LeetCode]Word Ladder II
- [Leetcode]Word Ladder
- [leetcode]Word Ladder
- [leetcode] Word Ladder
- [leetcode] Word Ladder II
- LeetCode-Word Ladder
- LeetCode- Word Ladder
- LeetCode - Word Ladder II
- 最初的梦想
- 方法(六)
- Qt写入txt文件
- 用qemu搭建linux环境的最简单步骤(硬盘启动)
- C++知识点随笔(二):继承、多态
- [Leetcode]Word Ladder
- thinkphp验证码在服务器显示不了
- 使用尾递归实现String To Int
- Android WebView
- Oracle中job的使用详解
- MinGW Msys Intro - Solution to not found gcc
- android开发中屏幕适配问题
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
- Ubuntu下安装PHP Mcrypt扩展