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"].

1.看到这个题目,我们第一部想到的就是要按照数字存储相对应的字母。这样的话,显然array是一个比较好的选择。根据测试, 数字1和0都是对应空 “”。
2.因为是不同的组合,所以我们要从不同数字的对应字母里分别找出一个字母进行组合,这个就和leetcode里combination的题目很相似,应该用recursion来解
解题思路如下:
比如说我们输入23:
那么按照我们的习惯, 我们从2中的abc选一个a 再到3中的def选一个d 好,完成ab,放入list里面, 返回
再到3中的def选一个e 好,完成ae,放入list里面 ,返回
                      再到3............f ...... af............ 返回,好了def里面我们已经结束了,返回到上一层的abc
我们从2中的abc选一个b
...............d .......bd............. 返回
                      ..............以此类推..................
           一直到我们遍历完abc
每当我们完成一个和数字组合相同的字符串 我们都存入list 并且return 并且返回到上层继续加入下一个属于同数字的字母
从上述我们可以看出recursion的逻辑
     public List<String> letterCombinations(String digits) {
String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
List<String> result = new ArrayList<>();
if (digits.length() == 0) {
return result;
}
formCombination(result, "", letters, 0, digits);
return result;
}
void formCombination(List<String> result, String current, String[]letters, int index, String digits) {
if (index >= digits.length() && current.length() == digits.length()) {
result.add(current);
return;
}
int digit = Integer.parseInt(digits.substring(index, index + 1));
char[] arr = letters[digit].toCharArray();
for (int j = 0; j < arr.length; j++) {
formCombination(result, current + arr[j], letters, index + 1, digits);
}
}


Leetcode - Letter Combination Of A Phone Number的更多相关文章

  1. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

  3. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. [LeetCode] Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. [LeetCode] Letter Combinations of a Phone Number(bfs)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. LeetCode Letter Combinations of a Phone Number (DFS)

    题意 Given a digit string, return all possible letter combinations that the number could represent. A ...

  8. [LeetCode] Letter Combinations of a Phone Number 回溯

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

随机推荐

  1. java封装性、继承性及关键字

    方法的参数传递(重点.难点)1.形参:方法声明时,方法小括号内的参数   实参:调用方法时,实际传入的参数的值 2.规则:java中的参数传递机制:值传递机制 1)形参是基本数据类型的:将实参的值传递 ...

  2. C# 实现语音听写

    本文系原创,禁止转载. 分享如何使用c#对接科大讯飞语音听写服务,简单高效地实现语音听写. 实现语音听写主要分为录音和语音识别两部分:录音是指获取设备声卡端口的音频数据并将之保存为音频文件,语音识别就 ...

  3. Klass与Oop

    前段时间,一直在看<Hotspot实战>,顺便编译了一份OpenJDK的源码,然后就在eclipse里面调试起来. 虽然我的入门语言是c/c++,但是被Java拉过来好几年了,现在再看源码 ...

  4. 51nod_1639:绑鞋带

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1639 #include <bits/stdc++.h& ...

  5. Python面向对象编程(四)

    1.多态 多态的概念虽然现在才说,但是我们一直在用.多态就是多种形态的意思,动物都猫,狗,猪等等,这些都是动物的多种形态. 反映在Python中,多态就意味着就算不知道变量所引用的对象类型是什么,也能 ...

  6. 百度地图JavaScript API使用

    最近在完成优达学城前端开发(入门)课程的P4项目中,要求调用google地图进行交互,项目已提供部分js代码和html代码.但在申请google地图API密钥时由于网络等原因,打不开或者连接超时,所以 ...

  7. 浅谈jQuery Pagination Ajax 分页插件的使用

    插件介绍 此插件是jQuery的ajax分页插件.分页切换时无刷新也无延迟,因为是一次性加载的.如果你用到此插件作分页的时候,涉及到的数据量大,建议不要使用此插件,因为加载慢会导致用户体验不好! 插件 ...

  8. OpenLayers3--ol3--新特性

    OL3: A high-performance, feature-packed library for all your mapping needs < 一个可以满足各种地图应用的高性能的.功能 ...

  9. 一步一步学习Vue(十一)

    本篇继续学习vuex,还是以实例为主:我们以一步一步学Vue(四)中讲述的例子为基础,对其改造,基于vuex重构一遍,这是原始的代码: todolist.js ; (function () { var ...

  10. 最通俗易懂的javascript变量提升

    a = 'ghostwu'; var a; console.log( a ); 在我没有讲什么是变量提升,以及变量提升的规则之前, 或者你没有学习过变量提升,如果按照现有的javascript理解, ...