[leetcode刷题系列]Word Ladder
来源:互联网 发布:淘宝怎样退出7天退换货 编辑:程序博客网 时间:2024/06/11 15:31
- - 囧啊, 这题卡了一下。 一直TLE。 原因为我每次扩展节点的时候都去dict里面搜索的。dict里面数据多的话就惨了。
然后正确的做法是枚举当前的改动位置,然后查找改动后是否在dict里面。怪不得dict是以hash的形式给出的,我刚看到的时候还
纳闷怎么用的hash不是vector- -我了个大囧的。
我还傻乎乎的每次扩展去dict遍历可以被扩展到的。
class Solution {public: int ladderLength(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function if(start == end) return 1; map<string, int> dist; queue<string> q; q.push(start); dist[start] = 0; while(!q.empty()){ string now = q.front(); q.pop(); for(int i = 0; i < now.size(); ++ i) for(char c = 'a'; c <= 'z'; ++ c){ string next = now; next[i] = c; if(next == end) return dist[now] + 2; if(dict.find(next) != dict.end() && dist.find(next) == dist.end()){ dist[next] = dist[now] + 1; q.push(next); } } } return 0; }};
- [leetcode刷题系列]Word Ladder
- [leetcode刷题系列]Word Ladder II
- leetcode:string hard系列二:word ladder
- [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
- WPF属性和资源
- java.io.EOFException是什么异常?
- 我只想说真强大强大
- CentOS 开启VNC与配置
- spring tool suite 开发MVC之hello word
- [leetcode刷题系列]Word Ladder
- IO流之二
- SICP 习题 (1.10)解题总结:阿克曼函数
- 开发者必备的6款源码搜索引擎
- hdu 4647
- Configuring and Using the Twisted Web Server
- PHP中获取目录及子目录下的所有文件路径和目录路径
- What is Linux Driver Model ?
- WPF视频播放