LeetCode 127 Word Ladder (广度优先搜索)
来源:互联网 发布:测试工具 python 编辑:程序博客网 时间:2024/06/06 02:20
最近重新捡起算法来看,发现广度优先搜索的代码不是很熟,就在leetcode上搜了一下,找到这道题练练,还是发现了很多问题的。
题目描述:
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 list
For example,
Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”]
As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
return its length 5.
由于事先知道是广度搜索,这道题目大体思路还是比较明确的,字典里存储可能的状态,定义数据结构存储队列中的字符串和当前的路径。代码如下:
class Solution {public: struct data { string s; int length; }; bool compareWords(string word1,string word2) { int length=word1.length(); int count=0; for(int i=0;i<length;i++) { if(word1[i]!=word2[i]) count++; } return count==1; } int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) { queue<data>q; data beginData; unordered_set<string> wordListCopy; beginData.s=beginWord;beginData.length=0; int length=0; q.push(beginData); while(!q.empty()) { data tempData; beginData=q.front(); q.pop(); if(compareWords(beginData.s,endWord)) return beginData.length+1; for(auto &word :wordList) if(wordListCopy.find(word)==wordListCopy.end()&&compareWords(beginData.s,word)) { tempData.s=word; tempData.length=beginData.length+1; q.push(tempData); wordListCopy.insert(word); } } return length; }};
上述代码运行最后超时了,但是感觉思路是对的,上网看了下别人的思路后,主要是在判断两个字符串是否只相差一个字符的时候算法复杂度为
class Solution {public: int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) { queue<string>q; q.push(beginWord); int length = beginWord.size(); int count = 1, level = 0; string s; while (!q.empty()) { s = q.front(); q.pop(); for (int i = 0; i < length;i++) for (char c = 'a'; c <= 'z'; c++) { if (c == s[i]) continue; swap(c, s[i]); if (s == endWord) return level+2; if (wordList.find(s) != wordList.end()) { q.push(s); wordList.erase(s); } swap(c, s[i]); } count--; if (count == 0) { count = q.size(); level++; } } return 0; }};
- LeetCode 127 Word Ladder (广度优先搜索)
- 【广度优先遍历】leetcode - Word Ladder II
- leetcode 127. Word Ladder BFS广度优先遍历
- 【广度优先遍历】Word Ladder II
- Word Ladder问题 BFS广度优先遍历
- leetcode 127 Word Ladder
- [leetcode 127]Word Ladder
- LeetCode(127) Word Ladder
- LeetCode 127 Word Ladder
- LeetCode(127) Word Ladder
- leetcode 127: Word Ladder
- 127 Word Ladder [Leetcode]
- LeetCode 127 Word Ladder
- Leetcode 127 Word Ladder
- leetcode 127 Word Ladder
- LeetCode 127 Word Ladder
- LeetCode 127 Word Ladder
- Leetcode-BFS广度优先搜索
- 数据科学中R VS Python:获胜者是...
- phpstorm快捷键
- 搜索—problem_1002-Strange fuction
- 自定义view之圆点progressBar
- Arcgis server 10.2 配置Oracle监听使用SQL操作ST_geometry
- LeetCode 127 Word Ladder (广度优先搜索)
- Linux NFS服务器的安装与配置
- C++之opencv将图片批量改成灰度图
- IRIS Online 编译过程
- API集合框架--泛型类
- 如何评价 APICloud ?
- 输入挂
- MAC快捷键
- AIDL 实现 IPC(进程间通信)