leetcode 17. Letter Combinations of a Phone Number
来源:互联网 发布:打开软件分辨率低 编辑:程序博客网 时间:2024/06/04 01:34
1.题目
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"].
根据电话上所按的数字,得到可能的字符的组合。
2.思路
我们可以创建一个字符串数组string letters[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 代表数字2-9所映射的字符。
要得到所有可能的解,自然的想到用dfs.
<pre name="code" class="cpp">class Solution {public: vector<string> ret; string letters[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; void dfs(int dep,int maxdep,string &s,string ans) { if(maxdep==0) return ; else if(dep == maxdep) {ret.push_back(ans);return;} for(int i=0;i<letters[s[dep]-'2'].size();i++) dfs(dep+1,maxdep,s,ans+letters[s[dep]-'2'][i]); } vector<string> letterCombinations(string digits) { dfs(0, digits.size(), digits,""); return ret; }};
0 0
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- iOS 项目配置
- 博客地址迁移
- 【Android】各机型分辨率对应app图标像素大小
- 小学生计算能力测试系统
- 深入理解 Hive
- leetcode 17. Letter Combinations of a Phone Number
- 利用 OSX 自带的 Automator 批量修改图片大小
- oracle job 的操作SQL
- DOCTYPE的作用,取值与区别
- KVC的用法
- 2015年iOS开发者收入调查报告--企业开发者
- git更新错误Can't update: no tracked branch
- font-awesome android html 字体库怎么引用。
- 第三周项目4-穷举法-辩数字