17. Letter Combinations of a Phone Number
来源:互联网 发布:淘宝买家注册 编辑:程序博客网 时间:2024/05/17 12:19
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"].
List的remove() 方法从头开始向外删除对象,add添加到后边。最巧妙的是利用了ans.peek().length()==i 来判断弹出的次数
先建立一个字符串数组,可以取每个数字对应的字符组成的字符串,将字符串变成字符数组 进行遍历向原来的数组添加
public class Solution { public List<String> letterCombinations(String digits) { LinkedList<String> ans = new LinkedList<String>(); String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; ans.add(""); for(int i =0; i<digits.length();i++){ int x = Character.getNumericValue(digits.charAt(i)); while(ans.peek().length()==i){ String t = ans.remove(); for(char s : mapping[x].toCharArray()) ans.add(t+s); } } return ans;}}
0 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
- 【IOS学习】设置某个界面强制横屏,进入就横屏的几种方法
- Android & iOS – on the edge of Qt and Java/Objective-C
- class_addIvar里的alignment属性
- C++——算法基础之排序——希尔排序(已修改)
- Java死锁的排查
- 17. Letter Combinations of a Phone Number
- 图解Windows域的命令行操作
- Android碎片 -- Fragment
- Web——Servlet操作Cookie
- JZOJ4759. 石子游戏
- ArryFilter过滤ListView
- POI
- 抽象类与接口的区别
- 问题集合