LeetCode OJ - Word Ladder

我觉得这道题比较难,主要是因为对于我来说:

1.
我没有把这个问题联想到树的宽度遍历(即便没有考虑树的宽度遍历,也是可以做的,但是我一开始实现的代码却是深度遍历,后来发现树的BFS一般使用queue实现的,貌似没有递归的方法??)

2. 即使在意识到用BFS,却还有一个陷阱:我是对字典进行了BFS,这个说实话,代码长,还TLE;

后来参考了前辈的代码,采用对26个单词进行枚举,才解决了这个问题。

下面是AC代码:


 1 /**
2 * Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end,
3 * BFS && 26 characters
4 * @param start
5 * @param end
6 * @param dict
7 * @return
8 */
9 public int ladderLength(String start, String end, HashSet<String> dict){
10 if(start.equals(end))
11 return 2;
12 //for BFS
13 LinkedList<String> queue = new LinkedList<String>();
14 //store the words that have visited and in the dict
15 HashSet<String> visited = new HashSet<String>();
16 //the level information for every word
17 LinkedList<Integer> level = new LinkedList<Integer>();
18 queue.offer(start);
19 level.offer(1);
20 while(!queue.isEmpty()){
21 String interW = queue.poll();
22 int le = level.poll();
23
24 //for every character in the word
25 for(int i=0;i<interW.length();i++){
26
27 char[] words = interW.toCharArray();
28 char o = words[i];//the original word
29 for(char c=‘a‘;c<=‘z‘;c++)
30 {
31 if(c!=o){
32 //subsitude by another char
33 words[i] = c;
34 String changed = new String(words);
35 if(changed.equals(end))
36 return le+1;
37 if((!visited.contains(changed)) && dict.contains(changed)){
38 visited.add(changed);
39 queue.offer(changed);
40 level.offer(le+1);
41 }
42 }
43 }
44
45 }
46 }
47 return 0;
48 }

LeetCode OJ - Word Ladder,码迷,mamicode.com

时间: 2024-10-11 04:04:30

LeetCode OJ - Word Ladder的相关文章

LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法. 这道题是在上一道的基础上进一步把所有可能得转换序列给出. 同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径. 下面是AC代码. 1 /** 2 * Given two words (start and end), and a dictionary, find all shortest transform

[LeetCode OJ] Word Search 深度优先搜索DFS

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

[LeetCode] 126. Word Ladder II_Hard tag: BFS&amp;DFS

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in the word list. Note

【LeetCode】Word Ladder 字符串

题目:Word Ladder <span style="font-size:18px;">/**LeetCode word ladder * 题目:给定一个起始单词和一个终结单词以及一个字典,要求每次变换一个字符,成为字典中新的词,直到变为最后的词,要求其最短路径 * 思路:利用队列,先弹出第一个词,分别将词中每一个字符替换直到找到一个字典中存在的词,加入队列,直到匹配的词是最后一个,此时终止 * 如果没有这样的路径,则返回0 */ package javaTrain; i

[LeetCode][JavaScript]Word Ladder

https://leetcode.com/problems/word-ladder/ Word Ladder Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time Each

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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

Leetcode 127. Word Ladder

思路: 1 class Solution(object): 2 def __init__(self): 3 return 4 5 def ladderLength(self, beginWord, endWord, wordList): 6 """ 7 :type beginWord: str 8 :type endWord: str 9 :type wordList: List[str] 10 :rtype: int 11 """ 12 fro

[Leetcode][JAVA] Word Ladder

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

leetcode 127. Word Ladder ----- java

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time Each intermediate word must exist in the word li