17. Letter Combinations of a Phone Number
来源:互联网 发布:测手速软件 编辑:程序博客网 时间:2024/05/22 01:49
Letter Combinations of a Phone Number
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
使用回溯法:
public class Solution { List<String> list; //需要全局的 public List<String> letterCombinations(String digits) { list=new ArrayList<String>(); if(digits.length()==0) return list; backTrack(digits,"",0); //digits第一个数字 return list; } void backTrack(String digits,String letter,int index){ if(index>=digits.length()){ list.add(new StringBuilder(letter).toString()); return; } int num = digits.charAt(index) - '2'; //键盘数字、字母是从2开始的 int end; if(num==5||num==7) end=4; //数字7和9是四个字母 else end=3; for(int i=0;i<end;i++){ char c; if(num<5) c=(char)('a'+(num*3+i)); else if(num==5) c=(char)('p'+i); //7 else if(num==6) c=(char)('t'+i); //8 else c=(char)('w'+i); //9 String newLetter = new StringBuilder(letter).toString(); backTrack(digits,letter+c,index+1); } }}
1 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
- Shell小记:lsmod
- 设置eclipse中resin的JVM参数
- Android .so相关
- autocomplete 属性在html <input autocomplete="on|off">中的使用
- Codeforces 612E - Square Root of Permutation
- 17. Letter Combinations of a Phone Number
- Android异步加载AsyncTask详解
- SNS 背后的技术: 消息流的推拉模式选择
- thinkphp 截取字符串
- 树莓派的无法USB调式
- Android 讲解:Handler
- 讲解 Spark API 最好的资料
- WPF禁止拖拽窗口到边缘自动最大化
- 核心动画之转场动画(CATransition)