题目如下:


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

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.


大概翻译:

给出一个整数组成的字符串,给出这些数字所能代表的所有字母组合。
数字和字母的映射关系(就像电话上的按钮一样)在下面的图片中给出。
注意:
尽管上面的答案是字典顺序,但是你的答案可以按照你想要的任何顺序来排列。

首先,考虑字母与数字有相应的映射关系,为了比较直观的展示,选择数组来存放会比较好。
其次,注意到数字的先后顺序对于字符串的结果是有影响的,后出现的数字可能代表的字符一定在先出现的数字可能代表的字符的后面,所以按顺序解析给定字符串就可以了。
【Java代码】如下:
public class Solution {public List<String> letterCombinations(String digits) {//定义数字和字母的对应关系char[] two = {'a', 'b', 'c'};char[] three = {'d', 'e', 'f'};char[] four = {'g', 'h', 'i'};char[] five = {'j', 'k', 'l'};char[] six = {'m', 'n', 'o'};char[] seven = {'p', 'q', 'r', 's'};char[] eight = {'t', 'u', 'v'};char[] nine = {'w', 'x', 'y', 'z'};List<String> result = new ArrayList<String>();//单独拿出String中的字符逐一判断for(int i = 0; i < digits.length(); i++){char ch = digits.charAt(i);if(ch == '2'){result = combat(result, two);}else if(ch == '3'){result = combat(result,three);}else if(ch == '4'){result = combat(result,four);}else if(ch == '5'){result = combat(result,five);}else if(ch == '6'){result = combat(result,six);}else if(ch == '7'){result = combat(result,seven);}else if(ch == '8'){result = combat(result,eight);}else if(ch == '9'){result = combat(result,nine);}}return result;}//连接字符的功能函数public List<String> combat(List<String> list, char[] sz){List<String> newList = new ArrayList<String>();if(list.size() <= 0){for(int i = 0; i < sz.length; i++){newList.add(sz[i] + "");}}for(int i = 0; i < list.size(); i++){String str = list.get(i);for(int j = 0; j < sz.length; j++){//在已有的字符串序列后加入新的字符newList.add(str + sz[j]);}}return newList;}
}

当然,上面的代码只是为了阅读方便,还可以进行优化。

优化方法就是创建一个二维数组,将上面的数组封装到一个二维数组中,直接根据给定的String字符串取出char后转换成数字下标取出对应字母数组。

具体实现方法交给读者自行实现。

如果有任何问题,欢迎跟我联系:xiaomenxiaomen@qq.com

我的github地址:github.com/WXRain

转载于:https://www.cnblogs.com/srain/p/7301406.html

LeeCode-------Letter Combinations of a Phone Number 解法相关推荐

  1. LeetCode | 0017. Letter Combinations of a Phone Number电话号码的字母组合【Python】

    LeetCode 0017. Letter Combinations of a Phone Number电话号码的字母组合[Medium][Python][回溯][DFS][暴力] Problem L ...

  2. Leetcode 17 - Letter Combinations of a Phone Number

    题目 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题意 已知在九宫格的输入法上,2对应的字符是a\b\c, ...

  3. LeetCode Letter Combinations of a Phone Number

    LeetCode解题之Letter Combinations of a Phone Number 原题 手机按键上每一个数字都相应了多个字母,如2相应了"abc",现给出一个数字串 ...

  4. 【DFS】LeetCode 17. Letter Combinations of a Phone Number

    LeetCode 17. Letter Combinations of a Phone Number Solution1:我的答案 利用8皇后同样的方法,回溯+递归 时间复杂度O(3n)O(3n)O( ...

  5. [ LeetCode ] #17. Letter Combinations of a Phone Number(电话按键组合字符 C++ Python)

    题目:17. Letter Combinations of a Phone Number Difficulty: Medium Given a string containing digits fro ...

  6. leetCode:Letter Combinations of a Phone Number

    题目 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinatio ...

  7. Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节

    1 题目理解 此道题目基于我们前几年用的,或许还有这几年用的九宫格输入法.九宫格上的每个数字对应着不同的字母,现在说,如果给定你一个数字,问你可以对应到多少种的字母组合? 这道题的解题方式,就是直接递 ...

  8. Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  9. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. centos6 mysql密码_CentOS6.5下修改MySQL密码
  2. websocket学习笔记
  3. 一步一步学Silverlight 2系列(27):使用Brush进行填充
  4. javascript优化--01高质量编码
  5. python怎么复数乘方开方_孩子数学不好怎么办?怎样让孩子学好数学的方法
  6. 【java学习之路】(java SE篇)(练习)集合练习:经典例题
  7. python def函数调用内容_基本的python,def函数和文本菜单的调用
  8. 照葫芦画瓢-string(字符)
  9. 计算机检测维修报告单,电脑维修检测报告表格.docx
  10. js 获取浏览器 滚动的高度
  11. Guarded Suspension模式
  12. php实训心得体会doc,php实训报告心得体会php实训报告心得体会
  13. 无法连接 树莓派 网线连接电脑_树莓派网线直连笔记本电脑
  14. android点击展开全文,Android显示全文折叠控件使用方法详解
  15. React Native学习资源汇总
  16. 上大学的四年---以此结束四年的时光
  17. 匿名科创--匿名拓空者PRO--开源光流融合算法使用教程
  18. 新闻发稿基本流程你知道么?
  19. 腾讯!网易!那些混不下去的互联网公司
  20. 【STM32H7教程】第78章 STM32H7的QSPI总线基础知识和HAL库API

热门文章

  1. 改变你一生命运的话语 不得不信
  2. MATLAB学习笔记(十四)
  3. 获取前一天的时间安排表_【央美考研】2021年硕士研究生招生入学考试时间安排...
  4. 【系统架构设计师】软考高级职称,来自订阅者真实反馈,从理论、实践、技巧让你掌握论文写作秘诀
  5. java 图片的路径_【JAVA技术】如何展现绝对路径下的图片
  6. Kubernetes通过一行shell命令给pod中的zk节点添加权限
  7. Java逆序列化报错serialVersionUID不同
  8. 提高CIFAR-10分类准确度的方法
  9. 原始的DSH深度哈希代码
  10. PHP小程序码扫码登录网站,WeAuth微信小程序实现PC网站扫码授权登录