LeetCode-17. Letter Combinations of a Phone Number
2017-02-12 16:24
483 查看
一、问题描述
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"].
二、解题思路
参考LeetCode论坛中的解题思路,使用LinkedList模拟FIFO队列。
三、代码如下
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"].
二、解题思路
参考LeetCode论坛中的解题思路,使用LinkedList模拟FIFO队列。
三、代码如下
public class Solution { public List<String> letterCombinations(String digits) { String[][] d2l={{""},{""},{"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","l"},{"m","n","o"},{"p","q","r","s"},{"t","u","v"},{"w","x","y","z"}}; LinkedList<String> result=new LinkedList<String>(); if(digits==null || digits.length()==0) return result; result.add(""); for(int i=0;i<digits.length();i++){ int temp=((int)digits.charAt(i))-48; while(result.peek().length()==i){ String t=result.remove(); for(int j=0;j<d2l[temp].length;j++){ result.add(t+d2l[temp][j]); } } } return result; } }代码中涉及到char字符转换为int的转化方法,是时候整理一下不同类型的变量相互转化的问题了
相关文章推荐
- 【leetcode】17. Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- LeetCode *** 17. Letter Combinations of a Phone Number
- 【LeetCode】17. Letter Combinations of a Phone Number
- Leetcode-17. Letter Combinations of a Phone Number
- [leetcode] 17. Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- leetcode--17. Letter Combinations of a Phone Number
- [LeetCode]--17. Letter Combinations of a Phone Number
- 个人记录-LeetCode 17. Letter Combinations of a Phone Number
- LeetCode - 17. Letter Combinations of a Phone Number
- 【leetcode】17. Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- 17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]
- LeetCode 17.Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number LeetCode
- leetcode 17. Letter Combinations of a Phone Number
- LeetCode-17.Letter Combinations of a Phone Number
- [LeetCode-Java]17. Letter Combinations of a Phone Number
- (Java)LeetCode-17. Letter Combinations of a Phone Number