算法描述:

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "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.

解题思路:

对于输出所有可能的组合这一类型题目,第一时间就应该想到采用回溯法。其中重点就是确定回溯的对象,本题中回溯的对象就是往结果中存的字符串。因为要考虑所有状况,所以处理完之后要还原回来。

    vector<string> letterCombinations(string digits) {vector<string> results;if(digits.size()<=0) return results;string dict[] = {"", "", "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};Combination(digits,0,results,dict, "");return results;}void Combination(string digits, int level, vector<string>& results, string dict[], string curr){if(level==digits.size()){results.push_back(curr);return;}int index = digits[level] - '0';string tmp = dict[index];for(int i=0; i < tmp.size(); i++){string next = curr + tmp[i];Combination(digits, level+1, results, dict, next);}}

转载于:https://www.cnblogs.com/nobodywang/p/10314982.html

LeetCode-17-Letter Combinations of a Phone Number相关推荐

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

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

  2. [ 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 ...

  3. Leetcode 17 - Letter Combinations of a Phone Number

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

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

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

  5. [LeetCode] 17. Letter Combinations of a Phone Number

    题目内容 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. https://leetcode-cn.com/p ...

  6. LeetCode 17. Letter Combinations of a Phone Number

    题意:给出数字串,输出对应的字母串组合,数字与字母的对应关系为手机上的 思路:使用递归,使用字符数组来存储递归选取时的字母,当到达数字串长度时,将字符数组转换为字符串 注意:数字串为空串时,输出为空 ...

  7. Leetcode 17. Letter Combinations of a Phone Number(python)

    笨方法回溯啦 class Solution(object):def letterCombinations(self, digits):""":type digits: s ...

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

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

  9. LeetCode 17. Letter Combinations of a Phone Number--笔试题--C++,Python解法

    题目地址:Letter Combinations of a Phone Number - LeetCode Given a string containing digits from 2-9 incl ...

  10. LeetCode:17. Letter Combinations of a Phone Number - Python

    问题描述: 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"2 ...

最新文章

  1. 每日一皮:你脑海中的项目 vs 你实际编写的代码
  2. 卷积的C语言实现的MFC版本
  3. ssh服务、密钥登陆配置
  4. JavaScript中十个一步拷贝数组的方法
  5. BZOJ1226 SDOI2009学校食堂(状压dp)
  6. “.NET研究”谈谈C# 4.0新特性“缺省参数”的实现
  7. 随想录(从kaldi学习语音识别)
  8. java数据库实体层封装_Java通过JDBC封装通用DAO层
  9. 认真测试直播软件,【转】如何测试直播软件
  10. Landsat8—ANG.txt文件
  11. 五笔字根表识别码图_识别码五笔字根表打印
  12. Informatic学习总结_day01
  13. 类 QQ IM 通讯软件开发实战
  14. mysql 用户 多次登录失败_限制用户登录失败次数,在连续登陆失败10次后冻结该用户。...
  15. 突破微信服务号群发限制
  16. 要求用户在Python中输入整数| 限制用户仅输入整数值
  17. 【BUCTOJ训练: 质数的和与积(Python)】
  18. java 依赖的项目报错_关于eclipse的maven项目Java Build Path中maven依赖报错问题
  19. 在win10+VS2019上编译支持Vulkan SDK的ncnn
  20. Process finished with exit code 132 (interrupted by signal 4: SIGILL)

热门文章

  1. 让你的AI绿起来,艾伦研究所提出深度学习效率评估标准Green AI
  2. var s=+newDate();
  3. 体系结构方案 -BI 系统的前置计算
  4. Java动态代理与Cglib代理
  5. day21 登录cookie
  6. oracle执行计划分析(转载)
  7. 军规13 降低流量和电量消耗
  8. Java基础第十三天总结
  9. 树莓派保卫战--防止SSH暴力破解
  10. 如何用两种方式同时实现ListBox的滚动功能