17. Letter Combinations of a Phone Number-回溯算法
来源:互联网 发布:安溪人很多做网络赌盘 编辑:程序博客网 时间:2024/06/05 04:07
原题链接:17. Letter Combinations of a Phone Number
【思路】
本题是回溯算法的基本应用。将整数值和对应的字母存入hashmap。
1.逐个取出digitals里的值,作为key在hashmap中找到val,取出val中的一个字符,并拼接在temp中
2.循环上述过程,直到digitals的最后一个字符,然后回溯:
public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); if (digits.length() == 0) return result; HashMap<Integer, String> map = new HashMap<Integer, String>(); 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"); searchCombinations(digits, 0, result, "", map); return result; } public void searchCombinations(String digits, int index, List<String> result, String temp, HashMap<Integer, String> map) { if (index == digits.length()) { result.add(temp); return; } String chars = map.get(digits.charAt(index) - '0'); for (int i = 0; i < chars.length(); i++) { temp += chars.charAt(i); searchCombinations(digits, index + 1, result, temp, map); temp = temp.substring(0, temp.length() - 1); } }25 / 25 test cases passed. Runtime: 1 ms Your runtime beats 46.02% of javasubmissions.
1 0
- 17. Letter Combinations of a Phone Number-回溯算法
- 17. Letter Combinations of a Phone Number 回溯算法
- 17. Letter Combinations of a Phone Number 组合、回溯
- leetcode 算法 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
- WCF入门教程——WCF的配置文件(四)
- Hidden Surface Removal / Visible Surface Determination
- WCF入门教程——WCF的通信模式(五)
- Matlab 图像像素点在RGB空间的显示
- [BZOJ 3585]mex
- 17. Letter Combinations of a Phone Number-回溯算法
- Linux并发(进程的生老病死)
- struts2中的addActionError 、addFieldError、addActionMessage的方法
- WCF入门教程——一个简单Demo(六)
- JDK配置环境变量的原因(windows)
- MongoDB2.0安装配置
- MongoDB:用户认证
- MongoDB之php操作
- 欢迎使用CSDN-markdown编辑器