LeetCode: Letter Combinations of a Phone Number 解题报告
2014-12-03 20:14
447 查看
[b]Letter Combinations of a Phone Number[/b]
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.
View Code
[b]GITHUB:[/b]
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/LetterCombinations.java
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.
public class Solution { String[] map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; public List<String> letterCombinations(String digits) { List<String> ret = new ArrayList<String>(); if (digits == null) { return ret; } dfs(digits, new StringBuilder(), ret, 0); return ret; } public void dfs(String digits, StringBuilder sb, List<String> ret, int index) { int len = digits.length(); if (index == len) { ret.add(sb.toString()); return; } // get the possiable selections. String s = map[digits.charAt(index) - '0']; for (int i = 0; i < s.length(); i++) { sb.append(s.charAt(i)); dfs(digits, sb, ret, index + 1); sb.deleteCharAt(sb.length() - 1); } } }
View Code
[b]GITHUB:[/b]
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/LetterCombinations.java
相关文章推荐
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- [leetCode 解题报告]080. Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number --leetcode解题报告
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
- LeetCode解题报告--Letter Combinations of a Phone Number
- leetcode 题解 || Letter Combinations of a Phone Number 问题
- leetcode 016 Letter Combinations of a Phone Number(Python)
- 【Leetcode】Letter Combinations of a Phone Number
- 【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】
- Leetcode 17 Letter Combinations of a Phone Number
- leetcode_17题——Letter Combinations of a Phone Number(简单题)
- leetcode——Letter Combinations of a Phone Number 手机按键字母组合(AC)
- LeetCode | Letter Combinations of a Phone Number(号码的字符串组合)
- LeetCode (17)Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路
- [LeetCode] Letter Combinations of a Phone Number