leetcode-algorithms-17 Letter Combinations of a Phone Number

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.

解法

通过数字对应的字符串,循环.

class Solution
{
public:
vector<string> letterCombinations(string digits)
{
std::vector<std::string> result;
if (digits.empty()) return result; result = {""};
std::vector<std::string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i = 0 ; i < digits.size(); ++i)
{
int num = digits[i] - '0';
if(num < 0 || num > 9)
break; std::string cand = v[num];
if(cand.empty())
continue; std::vector<std::string> tmp = result;
result.clear();
for(int j = 0; j < tmp.size(); ++j)
{
for(int k = 0; k < cand.size(); ++k)
result.push_back(tmp[j] + cand[k]);
}
}
return result;
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-17 Letter Combinations of a Phone Number的更多相关文章

  1. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  2. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

  4. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  5. 【LeetCode】17. Letter Combinations of a Phone Number

    题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...

  6. LeetCode:17. Letter Combinations of a Phone Number(Medium)

    1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...

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

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  8. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

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

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  10. [leetcode 17]Letter Combinations of a Phone Number

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

随机推荐

  1. AndroidStudio Gradle下载速度慢解决方法

    1.在软件里点开工程文件下的 build.gradle 2..在 buildscript 和 allprojects 的 repositories 中分别注释掉 jcenter() 3.在 build ...

  2. C#数字前面如何补0

    PadLeft()函数:向左补齐PadRight()函数:向右补齐 class Program { static void Main(string[] args) { ; Console.WriteL ...

  3. js多物体运动之淡入淡出效果

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  4. linux运行lnmp 出现502错误

    之前遇到的问题: 安装好之后访问域名出现502错误,打开html文件正常,说明是php出现问题.在wwwlog文件夹查看nginx日志,发现报错原因是找不到/var/run/php5-fpm.sock ...

  5. 单源最短路——Dijkstra模板

    算法思想: 类似最小生成树的贪心算法,从起点 v0 每次新拓展一个距离最小的点,再以这个点为中间点,更新起点到其他点的距离. 算法实现: 需要定义两个一维数组:①vis[ i ] 表示是否从源点到顶点 ...

  6. 使用phpmyadmin创建数据库

    1,使用phpmyadmin也需要实现安装php环境,安装环境请参考:http://www.sitestar.cn/bbs/thread-164-1-1.html: 2,到phpmyadmin官方网站 ...

  7. 小程序scss页面布局

    html <view class="main"> <form bindsubmit="feedback"> <textarea c ...

  8. NoSQL(not only struts query language)的简单介绍

    为什么需要NoSQL? 互联网自扩大规模来一直面临3个问题 1.High performance高并发 一个网站开发实时生成动态页面可能会存在高并发请求的需求,硬盘IO已经无法接受 2.Huge St ...

  9. leecode第一百二十一题(买卖股票的最佳时机)

    class Solution { public: int maxProfit(vector<int>& prices) { int len=prices.size(); ) ; v ...

  10. 《剑指offer》第六十一题(扑克牌的顺子)

    // 面试题61:扑克牌的顺子 // 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. // 2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意 ...