leetcode-第四周
来源:互联网 发布:成人学汉语拼音软件 编辑:程序博客网 时间:2024/06/05 17:10
126. Word Ladder II
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 that beginWord is not a transformed word.
For example,
Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”,”cog”]
Return
[
[“hit”,”hot”,”dot”,”dog”,”cog”],
[“hit”,”hot”,”lot”,”log”,”cog”]
]
Note:
Return an empty list 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.
思路:深搜最短转换+广搜答案,单词哈希表最好用unordered_set,否则会MLE
/** * 思路:深搜最短转换+广搜答案,单词哈希表最好用unordered_set,否则会MLE */class Solution {private: void bfs(string &beginWord, unordered_map<string, int> &dep, unordered_set<string> &has) { dep[beginWord] = 1; queue<string> que; que.push(beginWord); while (!que.empty()) { string cur = que.front(); //cout << cur << " " << dep[cur] << endl; que.pop(); for (int i = 0; i < cur.size(); i++) { for (int j = 'a'; j <= 'z'; j++) { string nxt(cur); nxt[i] = j; if (has.find(nxt) == has.end()) continue; if (dep[nxt]) continue; que.push(nxt); dep[nxt] = dep[cur] + 1; } } } } void dfs(vector<vector<string> > &ret, vector<string> ¤t, string &end, unordered_map<string, int> &dep, unordered_set<string> &has) { current.push_back(end); //cout << end << " " << dep[end] << endl; if (dep[end] == 1) { vector<string> tmp(current); reverse(tmp.begin(), tmp.end()); ret.push_back(tmp); } else { for (int i = 0; i < end.size(); i++) { for (int j = 'a'; j <= 'z'; j++) { string pre(end); pre[i] = j; if (has.find(pre) == has.end()) continue; //cout << " " << pre << " " << dep[pre] << endl; if (dep[pre] + 1 != dep[end]) continue; dfs(ret, current, pre, dep, has); } } } current.pop_back(); }public: vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { unordered_map<string, int> dep; unordered_set<string> has; has.insert(beginWord); for (auto w: wordList) has.insert(w); bfs(beginWord, dep, has); vector<vector<string> > ret; vector<string> cur; if (dep.find(endWord) != dep.end()) dfs(ret, cur, endWord, dep, has); return ret; }};
187. Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”,
Return:
[“AAAAACCCCC”, “CCCCCAAAAA”].
思路:注意题目中要求得串长度为10,所以直接用滑动窗口+哈希即可
/* * Time: O(n) * Space: O(n) * 思路:注意题目中要求得串长度为10,所以直接用滑动窗口+哈希即可 */class Solution {public: vector<string> findRepeatedDnaSequences(string s) { unordered_map<string, int> mp; vector<string> ret; for (int i = 0; i + 9 < s.size(); i++) { string tmp = s.substr(i, 10); mp[tmp]++; if (mp[tmp] == 2) ret.push_back(tmp); } return ret; }};
17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
思路:深搜暴力求解
/* * Time: O(4 ^ n) * Space: O(n) * 思路:深搜暴力求解 */class Solution {private: void dfs(const string &digits, int pos, vector<string> &ret, string ¤t) { const char* l[10] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; if (pos == digits.size()) { ret.push_back(current); return; } int val = digits[pos] - '0'; for (int i = 0; i < strlen(l[val]); i++) { current.push_back(l[val][i]); dfs(digits, pos + 1, ret, current); current.pop_back(); } }public: vector<string> letterCombinations(string digits) { vector<string> ret; if (digits.empty()) return ret; string current = ""; dfs(digits, 0, ret, current); return ret; }};
- leetcode-python 第四周
- leetcode-第四周
- 第四周leetcode题
- [leetcode] 第四周作业
- LeetCode题解 第四周
- 第四周LeetCode
- leetcode第四周解题报告
- LeetCode 算法习题 第四周
- LeetCode 算法习题 第四周
- 第四周LeetCode算法题两道
- 第四周:[leetcode] 126. Word Ladder II
- leetcode【第四周】 交换节点对
- 第四周:[Leetcode]207. Course Schedule
- 第四周:[Leetcode]210. Course Schedule II
- 第四周:[LeetCode]332. Reconstruct Itinerary
- 第四周
- 第四周
- 第四周
- SQL语句UNION和 UNION ALL
- cordova/phoneGap
- App更新(Android)
- 原创技术思维文章大纲
- 2017.03.14回顾
- leetcode-第四周
- devDependencies和dependencies的区别
- Android优化
- input属性onpaste和oncontextmenu
- js基础9应用案例
- 关于AndroidStudio绑定GitHub后项目包变大问题
- 通过Python脚本创建ArcGIS地图服务缓存
- 关于QT的系统总结
- java各种读取properties文件方法