17. Letter Combinations of a Phone Number
来源:互联网 发布:上市公司数据在哪里查 编辑:程序博客网 时间:2024/06/05 10:13
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.
class Solution { public void dfs(String digits, HashMap<Integer, String> map, List<String> lst, StringBuilder s, int begin){ if ( begin == digits.length()){ lst.add(new String(s)); } else { String tmp = map.get(digits.charAt(begin) - '0'); for (int i = 0; i < tmp.length(); ++ i){ s.append(tmp.charAt(i)); dfs(digits, map, lst, s, begin + 1); s.deleteCharAt(s.length() - 1); } } } public List<String> letterCombinations(String digits) { HashMap<Integer, String> map = new HashMap<>(); map.put(0, ""); map.put(1, ""); map.put(2, "abc"); map.put(3, "def"); map.put(4, "ghi"); map.put(5, "jkl"); map.put(6, "mno"); map.put(7, "pqrs"); map.put(8, "tuv"); map.put(9, "wxyz");List<String> lst = new ArrayList<>(); if (digits.length() == 0){ return lst; } StringBuilder s = new StringBuilder(); dfs(digits, map, lst, s, 0); return lst; }}
阅读全文
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
- Jquery总结
- 【第四周项目3】单链表应用(三)
- 电脑里那个自带的游戏老是无响应,或者打开很慢是怎么了
- oracle错误之TNS12541无监听程序
- Video Surveillance POJ
- 17. Letter Combinations of a Phone Number
- MySQL下载与安装
- cogs2632. [HZOI 2016]数列操作d
- 对__stdcall和__cdecl的简单了解
- 实现用鼠标拖动没有标题栏的窗口(窗体的任何部分都可实现拖动效果)
- 如何在AMDP中使用SELECT-OPTION
- Git 分支管理和冲突解决
- Java基础的一些小问题
- JavaWeb中Model1简介