首页 > 代码库 > [LeetCode] Letter Combinations of a Phone Number
[LeetCode] 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.
好长时间没练题了,最近比较懒加之工作略忙,疏于思考,浪费了很多时间。这题想来想去知道是要深搜,但是都没动手实现,以为很复杂,其实是很典型的DFS模板,这和 Sudoku solver 的思路基本没有差别(其实和所有类似的题型都没差,几乎都用DFS模板一套就能出来)
void comboIter(string digits, int i, vector<string> &domains, string cur, vector<string> &result) { if (i >= digits.size()) { result.push_back(cur); return; } char t = digits.at(i); string domain = domains[t-‘0‘]; string old = cur; for (char ele : domain) { cur += ele; comboIter(digits, i+1, domains, cur ,result); cur = old; }}vector<string> letterCombinations(string digits) { vector<string> ret; if (!digits.size()) return {""}; vector<string> domain = {"0","1","abc","def","ghi","jkl","mno","pqr","stu","vwxyz"}; comboIter(digits,0,domain,"",ret); return ret;}
[LeetCode] Letter Combinations of a Phone Number
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。