leetcode-17. Letter Combinations of a Phone Number
2017-06-10 20:15
369 查看
https://leetcode.com/problems/letter-combinations-of-a-phone-number/#/description
问题描述:
思路解析:
将每个按键代表的字符串映射到一个string数组当中,下标对应着按键的顺序。将组合的结果存到LinkedList当中,模拟FIFO队列,每次取出队头元素,然后和其他按键的元素进行组合。
代码如下:
问题描述:
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路解析:
将每个按键代表的字符串映射到一个string数组当中,下标对应着按键的顺序。将组合的结果存到LinkedList当中,模拟FIFO队列,每次取出队头元素,然后和其他按键的元素进行组合。
代码如下:
public class Solution { public List<String> letterCombinations(String digits) { LinkedList<String> result=new LinkedList<String>(); if(digits.length()==0) return result; String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; result.add(""); for(int i=0;i<digits.length();i++) { int k=Character.getNumericValue(digits.charAt(i)); while(result.peek().length()==i){ String t=result.remove(); for(char s:mapping[k].toCharArray()) { result.add(t+s); } } } return result; } }
相关文章推荐
- Leetcode-17. Letter Combinations of a Phone Number
- 【leetcode】17. Letter Combinations of a Phone Number (script)版本答案
- leetcode 17. Letter Combinations of a Phone Number 按照indexDFS深度优先遍历
- [leetcode]17. Letter Combinations of a Phone Number
- LeetCodet题解--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(medium)
- leetcode第17题 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 字符串 | 回溯 | Medium
- LeetCode 17. Letter Combinations of a Phone Number(电话号码→字母组合)
- leetcode 17. Letter Combinations of a Phone Number
- [leetcode] 17. Letter Combinations of a Phone Number ,python实现【medium】
- LeetCode17. Letter Combinations of a Phone Number可能是世界上最简明的解法了
- leetcode - 17.Letter Combinations of a Phone Number
- LeetCode 17. Letter Combinations of a Phone Number