【leetcode】【17】Letter Combinations of a Phone Number
来源:互联网 发布:织梦 文章分页 index 编辑:程序博客网 时间:2024/06/03 08:43
一、问题描述
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"].
二、问题分析
典型的回溯法问题。采用回溯的通用解决方式,只不过需要注意的是将digits映射到字符串上,所以可以使用字符串数组,数组的索引与数字恰好有对应关系。
三、Java AC 代码
public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<String>(); if(digits==null || digits.length()==0){ return list; } String[] nums = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; dfsHelper(list,new String(), 0, digits, nums); return list; } public void dfsHelper(List<String> list, String item, int start, String digits, String[] nums){ if(item.length()==digits.length()){ list.add(item); return ; } int num = digits.charAt(start) - '0'; String tmp = nums[num-2]; for(int j = 0; j< tmp.length(); j++){ dfsHelper(list, item+tmp.charAt(j), start+1, digits, nums); } }
0 0
- LeetCode: Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [Leetcode] Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- LeetCode:Letter Combinations of a Phone Number
- LeetCode | Letter Combinations of a Phone Number
- leetcode Letter Combinations of a Phone Number
- Leetcode: Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- leetcode: Letter Combinations of a Phone Number
- 进程间的通信
- 【小镇的技术天梯】刷票与防刷票的思考
- Android中怎么切换task到前台
- 自定义ViewGroup之卫星菜单
- VC模态和非模态对话框关闭过程
- 【leetcode】【17】Letter Combinations of a Phone Number
- 快速掌握Lua 5.3 —— I/O库 (1)
- linux下使用yum安装mysql
- 阻塞与非阻塞I/O
- React Native学习之TabBarIOS用法
- 用最少的代码写出求3个数最大值的函数
- 今日BBC
- (转)Django 1.6 最佳实践: 如何正确的使用和设置Database和Model
- Hibernate防止SQL注入攻击的方法