17. Letter Combinations of a Phone Number(bfs)
2024-10-12 12:39:38
Given a string containing digits from
2-9
inclusive, 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. Note that 1 does not map to any letters.
Example:
Input: "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.
class Solution {
public List<String> letterCombinations(String digits) {
LinkedList<String> res = new LinkedList<String>();
if(digits.isEmpty()) return res;
String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
res.add("");
for(int i=0;i<digits.length();i++){
int x=Character.getNumericValue(digits.charAt(i));
while(res.peek().length()==i){
String t = res.remove();
for (char s :mapping[x].toCharArray())
res.add(t+s);
}
}
return res;
}
}
最新文章
- java的英文词频算法
- 使用winpcap多线程抓包,以及简单的分析数据包
- python公司面试题集锦 python面试题大全
- IPv6介绍
- (转)Java Ant build.xml详解
- [Protractor] Getting Started With Protractor
- asp.net MVC Razor 语法(2)
- PHP操作Mysql中间BLOB场
- 鸟哥Linux私房菜知识点总结6到7章
- JAVA-面向对象--封装
- RGB565的理解
- HttpStatus各种状态
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
- C# DropDownList 绑定枚举类
- easyui常见问题
- jQuery toastr提示简单实现
- hadoop集群无法找到datanode节点问题解决
- 目标检测算法之R-CNN算法详解
- div在页面垂直居中方法---增强改进版
- GIS中的数据库.gdb与.mdb的区别