博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
127. Word Ladder
阅读量:2351 次
发布时间:2019-05-10

本文共 2728 字,大约阅读时间需要 9 分钟。

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 transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:

beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]

Output: 5

Explanation: As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
return its length 5.

我的想法

刚拿到这道题的我没有什么想法,头脑一片空白

解答

jiuzhang solution:

隐式图,每次只能变一个字符,两个邻接单词之间肯定只有一个字符不一样。
虽然知道怎么做了,但是还是不知道为什么这道题可以想到用图来做

class Solution {
public int ladderLength(String beginWord, String endWord, List
wordList) {
if(beginWord.equals(endWord)) {
return 1; } //构造查询集合dict Set
dict = new HashSet<>(); for(String word : wordList) {
dict.add(word); } //bfs Set
visited = new HashSet<>(); Queue
queue = new LinkedList<>(); queue.offer(beginWord); visited.add(beginWord); int length = 1; while(!queue.isEmpty()) {
length++; //路径长度 int size = queue.size(); for(int i = 0; i < size; i++) {
String cur = queue.poll(); //获取邻接词 for(String word : getNext(dict, cur)) {
if(visited.contains(word)) {
continue; } if(endWord.equals(word)) {
return length; } queue.offer(word); visited.add(word); } } } return 0; } /* getNext()有两种写法: N:单词总数 L:单词长度 1. for(word 单词总数) O(N) 比较当前单词cur与dict中的单词word是否只相差一个字符 O(L) 2. for(单词长度) O(L) for(25个小写字符) 25 生成新单词 O(L) 比较新单词是否在dict中 O(L) 第1种方式O(N*L); 第2种方式O(L*25*2L)=>O(50*L^2) 如果N >> L,则第二种方式更好,否则第一种更好 */ private ArrayList
getNext(Set
dict, String cur) { ArrayList
next = new ArrayList<>(); //对字符串cur的每一个字符都进行枚举替换 for(int i = 0; i < cur.length(); i++) { for(char c = 'a'; c <= 'z'; c++) { char[] chars = cur.toCharArray(); //跳过自身 if(c == chars[i]) { continue; } chars[i] = c; String str = new String(chars); //如果有,说明str与cur邻接 if(dict.contains(str)) { next.add(str); } } } return next; }}

转载地址:http://vkqvb.baihongyu.com/

你可能感兴趣的文章
漫谈兼容内核之十二:Windows的APC机制
查看>>
21.windbg-.lastevent、!analyze(dump分析、异常错误码查询)
查看>>
16.windbg-.frame、dt(切换局部上下文、查找结构体)
查看>>
为何new出的对象数组必须要用delete[]删除,而普通数组delete和delete[]都一样-------_CrtMemBlockHeader
查看>>
windbg调试命令:!peb、!teb
查看>>
开源任务管理器 Process Hacker (Windows)
查看>>
快速发现Windows中毒的工具:Process Hacker
查看>>
Process Hacker源码中的用户态hook的做法
查看>>
Get IT技能知识库 50个领域一键直达
查看>>
浅析C++中的this指针及汇编实现
查看>>
关于32位程序在64位系统下运行中需要注意的重定向问题(有图有真相)(***)
查看>>
解决win10系统中截图异常放大的问题
查看>>
关于Windows高DPI的一些简单总结
查看>>
tlb文件为何而生?
查看>>
IE9 GPU硬件加速到底是实用创新还是噱头
查看>>
UpdateLayeredWindow与SetLayeredWindowAttributes的使用
查看>>
IM消息送达保证机制实现(二):保证离线消息的可靠投递
查看>>
bss段、data段和text段
查看>>
程序或-内存区域分配(五个段)--终于搞明白了
查看>>
PE总结 - DOS文件头、PE文件头、节表和表详解
查看>>