LeetCode Word Ladder解题报告
来源:互联网 发布:mcs51单片机编程环境 编辑:程序博客网 时间:2024/06/05 16:25
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, 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.
题不难理解,输入的是:一个单词和一个字典,单词每次只允许改变一个字母变成字典中的某个单词,如此循环,判断是否能变成目标单词,如果能的话返回变换次数,否则返回0。
解题报告:
最直观的思路就是DFS,每次变成字典中的某个新单词,同时从字典中删除这个单词然后不断递归。
大数据时候超时。因为要求的是最短的变换次数,所以可以使用BFS,和DFS不一样不一次走到最深。逐层遍历变换了一次、二次、三次、n次的所有单词。
思考一下DFS和BFS的区别,举个最简单的例子,111 -> 311。
DFS的话,111->112,之后需要DFS 112的所有变形。同理111->113之后还要遍历113的所有变形。
上AC代码:
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.
题不难理解,输入的是:一个单词和一个字典,单词每次只允许改变一个字母变成字典中的某个单词,如此循环,判断是否能变成目标单词,如果能的话返回变换次数,否则返回0。
解题报告:
最直观的思路就是DFS,每次变成字典中的某个新单词,同时从字典中删除这个单词然后不断递归。
大数据时候超时。因为要求的是最短的变换次数,所以可以使用BFS,和DFS不一样不一次走到最深。逐层遍历变换了一次、二次、三次、n次的所有单词。
思考一下DFS和BFS的区别,举个最简单的例子,111 -> 311。
DFS的话,111->112,之后需要DFS 112的所有变形。同理111->113之后还要遍历113的所有变形。
而BFS的话, 111
112 113 121 131 211 311
只需要6次就能找到最终结果。
上AC代码:
public class Solution {public int ladderLength(String start, String end, HashSet<String> dict) { if(start.equals(end)==true||start==null||end==null) { return 0; } Queue queue = new LinkedList<String>(); //记录当前节点所在的层数,也就是经过了多少次变换 LinkedList<Integer> distanceQueue = new LinkedList<Integer>(); queue.add(start); distanceQueue.add(1); int ret = 0; while(!queue.isEmpty()) { String str = (String)queue.poll(); ret = (int)distanceQueue.poll(); //对当前单词的每个字符逐个测试,生成的新单词是否存在于字典中 for(int i=0; i<str.length(); i++){ char[] strCharArr = str.toCharArray(); for(char c='a'; c<='z'; c++){ strCharArr[i] = c; String newWord = new String(strCharArr); if(dict.contains(newWord)){ if(newWord.equals(end)) { return ret + 1; } queue.add(newWord); distanceQueue.add(ret + 1); dict.remove(newWord); } } } } return 0; }}
0 0
- LeetCode Word Ladder解题报告
- [LeetCode]Word Ladder,解题报告
- LeetCode Word Ladder解题报告
- Leetcode Word Ladder II 解题报告
- leetcode 解题报告 Word Ladder II
- [leetcode] 127. Word Ladder 解题报告
- [leetcode] 126. Word Ladder II 解题报告
- [Leetcode] 126. Word Ladder II 解题报告
- [Leetcode] 127. Word Ladder 解题报告
- [leetcode]127. Word Ladder@Java解题报告
- Leetcode 127. Word Ladder 字符变换 解题报告
- Leetcode 126. Word Ladder II 字符变换2 解题报告
- leetcode解题笔记-word-ladder (python版)
- leetcode解题笔记:127. Word Ladder
- 北大校赛2011 Word Ladder(C题) 解题报告
- Leetcode Word Search 解题报告
- LeetCode Word Break 解题报告
- 【LeetCode】Word Break 解题报告
- 设计模式之-----------------外观模式
- C++学习笔记(八)----表(list)的实现
- [js] Javascript中的一种深复制实现
- 微信公众平台开发教程第20篇-新手解惑40则
- 微信公众平台开发教程第21篇-“可信网址”白名单
- LeetCode Word Ladder解题报告
- 英雄会(csdn pongo)题解之半质数的个数--2·14情人&元宵节专题
- 浅谈组织执行力修炼
- [js] 如何循序渐进地学习Javascript
- Java注解的入门知识
- 提升Android ListView性能的几个技巧
- MultiplicationTable
- 直销软件的开发(实例)
- 插入排序