Letter Combinations of a Phone Number 电话按键字符的组合@LeetCode
来源:互联网 发布:产业结构优化论文 编辑:程序博客网 时间:2024/04/29 17:06
先搞一张电话数字和字符的对应表,再针对给定电话号码DFS回溯
package Level3;import java.util.ArrayList;/** * 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.http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.pngInput: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. * */public class S17 {public static void main(String[] args) {System.out.println(letterCombinations("23"));}public static ArrayList<String> letterCombinations(String digits) {String[] ss = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};ArrayList<String> ret = new ArrayList<String>();rec(ret, digits.length(), ss, digits, new StringBuffer());return ret; }public static void rec(ArrayList<String> ret, int remain, String[] ss, String digits, StringBuffer sb){// 说明对digits遍历结束if(remain == 0){ret.add(sb.toString());return;}String s = ss[digits.charAt(0)-'0'];// 得到digits[0]对应的string s// DFSfor(int i=0; i<s.length(); i++){sb = sb.append(s.charAt(i));rec(ret, remain-1, ss, digits.substring(1), sb);sb.deleteCharAt(sb.length()-1);// 恢复现场}}}
public class Solution { public List<String> letterCombinations(String digits) { String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; List<String> list = new ArrayList<String>(); rec(digits, map, list, new StringBuilder(), 0); return list; } public void rec(String digits, String[] map, List<String> list, StringBuilder sb, int pos) { if(pos == digits.length()) { list.add(sb.toString()); return; } String s = map[digits.charAt(pos)-'0']; for(int i=0; i<s.length(); i++) { sb.append(s.charAt(i)); rec(digits, map, list, sb, pos+1); sb.deleteCharAt(sb.length()-1); } }}
- Letter Combinations of a Phone Number 电话按键字符的组合@LeetCode
- Letter Combinations of a Phone Number 电话按键的组合
- [LeetCode]-Letter Combinations of a Phone Number 电话键盘的字符串组合
- leetCode 17.Letter Combinations of a Phone Number(电话数字对应的字母组合) 解题思路和方法
- Letter Combinations of a Phone Number (电话号码字母组合) 【leetcode】
- LeetCode Letter Combinations of a Phone Number 经典组合问题
- 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
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)
- 需求决定选择
- JavaScript自定义方法实现trim()、Ltrim()、Rtrim()的功能
- javascript的trim,ltrim,rtrim自定义函数
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
- Letter Combinations of a Phone Number 电话按键字符的组合@LeetCode
- 动态规划
- How can I access my files via FTP on Ubuntu?
- 最小生成树-Kruscal算法
- 与陈总聊的,国家形势与个人选择
- POJ:1111 Image Perimeters
- String StringBuffer和StringBuilder
- hibernate之openSession()与getCurrentSession()的区别
- WebService——通过契约优先开发webservice