[LeetCode] Letter Combinations of a Phone Number
来源:互联网 发布:北京北大青鸟网络学校 编辑:程序博客网 时间:2024/06/05 03:40
这个问题大概是考察递归,写了一个递归的算法。主要是输入没有考虑“”,也没有想到输出也要求[""],其他代码基本上没有问题。
public class Solution {static String[][] keyMap = { { " " }, {}, { "a", "b", "c" },{ "d", "e", "f" }, { "g", "h", "i" }, { "j", "k", "l" },{ "m", "n", "o" }, { "p", "q", "r", "s" }, { "t", "u", "v" },{ "w", "x", "y", "z" } }; public ArrayList<String> letterCombinations(String digits) {ArrayList<String> result = new ArrayList<String>();if (digits == null || digits.isEmpty()) {result.add("");return result;}int length = digits.length();if (length == 1) {int num = Integer.valueOf(digits);String[] kMap = keyMap[num];for (int i = 0; i < kMap.length; ++i) {result.add(kMap[i]);}} else {ArrayList<String> subResult = letterCombinations(digits.substring(1, length));String first = digits.substring(0, 1);int num = Integer.valueOf(first);String[] kMap = keyMap[num];for (int i = 0; i < kMap.length; ++i) {for (int j = 0; j < subResult.size(); ++j) {result.add(kMap[i] + subResult.get(j));}}}return result; }}
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
- C++ Primer 第八章 标准IO库
- cookie与session以及他们的常规用途——web开发必须熟知的知识
- android 虚拟设备(AVD)的创建和使用
- 【最小生成树】
- EL表达式语言
- [LeetCode] Letter Combinations of a Phone Number
- 在一个文件中有10G个整数,乱序排列,要求找出中位数
- 批量删除Windows7中隧道适配器的方法
- Distinct Subsequences
- Parallels 9 Activation:
- Buttons(poj 2368)
- 10006Carmichael Numbers
- HDU1881 - 毕业bg(背包大小会改变的01背包)
- [c.y.j]redis 错误提示