17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝香水店铺推荐 知乎 编辑:程序博客网 时间:2024/06/11 22:27
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"].思路:排列组合的分期摊还(amortized)方法
1 从数字string一个数字开始扫描,求出当前数字对应的字母; 如2对应"abc"
2 把每个映射的字母,依附到结果集res的每个组合中,产生新的结果集,并返回;
3 扫描至数字结尾时,结果集就是所求的所有组合;
//amortized方法, 分期摊还法 public IList<string> LetterCombinations(string digits) { IList<string> res=new List<string>(); if(digits==null||digits.Length==0) return res; //初始化,先加入空" res.Add(""); //数字对应的字母0-9,其中0-1不对应任何字母 string[] mapArr=new string[]{"","","abc","def","ghi","jkl","mno","qprs","tuv","wxyz"}; char[] charArr=digits.ToCharArray(); for(int i=0; i<charArr.Count(); i++) { //获取该char对应的string string curStr = mapArr[charArr[i]-'0']; char[] curStrArr=curStr.ToCharArray(); IList<string> tmp=new List<string>(); for(int j=0;j < curStrArr.Count();j++) //把每个对应的字母加入到每个结果集合 for(int k=0;k<res.Count();k++) { string TmpRes=(res[k] + curStrArr[j]).ToString(); tmp.Add(TmpRes); } //保存最新结果集 res=tmp; } return res; }
0 0
- 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- [leetcode]328. Odd Even Linked List [medium]
- php源码之路第三章第二节( 常量)
- 第四天学习数据结构,完成一半的逆波兰表达式
- 大型网站架构之分布式消息队列
- struts2基础学习总结
- 17. Letter Combinations of a Phone Number
- jQuery最简单的留言功能^-^
- c#学习 11/22 里式转换与常用类的使用
- java线程内存模型
- Android ORMLite数据库框架使用入门
- JAVA方法详解
- 前端提升之Gulp构建
- HTML语义化
- js实现窗口全屏示例