leetcode017 Letter Combinations of a Phone Number
来源:互联网 发布:手机优酷网络连接失败 编辑:程序博客网 时间:2024/06/07 07:10
题目
17. Letter Combinations of a Phone Number
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"].
思路:
利用递归求解。
代码:
private List<String> out = new ArrayList<>();private List<String> in = new ArrayList<>();private String[] resources = {"", "", "abc", "def", "ghi", "jkl", "mon", "pqrs", "tuv", "wxyz"};public List<String> letterCombinations(String digits){ if(digits == null || digits.length() == 0 || digits.contains("1") || digits.contains("0")) return out; char[] c_digits = digits.toCharArray(); for(char c_digit : c_digits) in.add(resources[c_digit - '0']); getString("", 0); return out;}private void getString(String builder, int index){ char[] chars = in.get(index).toCharArray(); if(index == in.size() - 1) for(char c : chars) out.add(builder + c); else for(char aChar : chars) getString(builder + aChar, index + 1);}
结果细节(图):
0 0
- leetcode017 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
- LeetCode18:Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- 【leetcode】Letter Combinations of a Phone Number
- LeetCode: Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]Letter Combinations of a Phone Number
- LeetCode-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode] Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- LeetCode - Letter Combinations of a Phone Number
- 【心血之作】linux虚拟机下安装配置Hadoop(完全分布式)生态环境(hadoop2.2.0,HBase0.98,Hive0.13(连接oracle),sqoop1.4.4(连接oracle)
- 大数据量高并发的数据库优化
- POJ1275 Cashier Employment
- CROC 2016 - Elimination Round B 树状数组处理逆序对个数
- Python itertools模块详解
- leetcode017 Letter Combinations of a Phone Number
- VBA基础
- Linux 上的常用文件传输方式介绍与比较
- c语言学习路程 冒泡排序算法void bubble(int i )函数
- 实用的 Python 之 feedparser
- JavaScript语言精粹——数组
- nRF52832之硬件I2C
- 【1】JAVA---地址App小软件(AddressApp.class)(初步接触项目开发的分层思想)(表现层)
- 设计模式学习笔记——命令模式