一天一道LeetCode

(一)题目

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

(二)解题

这题采用回溯法。

以23为例,2对应“abc” ,3对应“def。

第一步:a,b,c

第二部:ad,ae,af,bd,be,bf,cd ,ce,cf

回溯法的意思是,依次递归到最后,如果到最后了就回溯,继续递归。

算法的过程:

首先依次递归得到ad,到digits的长度了,回溯得到a

这下又可以递归了,得到ae,又到digits的长度了,再回溯到a

然后继续循环得到af,f到头了,a也到头了,轮到b上场了!

…….依次下去就把所有的情况都输出了!

class Solution {
public:
    string phoneStr[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    string tmp;
    vector<string> result;
    vector<string> letterCombinations(string digits) {
        if(digits.length()==0) return result;
        dfs(digits , 0);
        return result;
    }
    void dfs(string &str , int idx)
    {
        if(idx == str.length())
        {
            result.push_back(tmp);//递归结束条件
            return;
        }
        else
        {
            for(int i = 0 ; i < phoneStr[str[idx]-'0'].length() ; i++)
            {
                tmp = tmp.substr(0,idx);//回溯!
                tmp+=(phoneStr[str[idx]-'0'])[i];
                dfs(str,idx+1);
            }
        }

    }
};

【一天一道LeetCode】#17. Letter Combinations of a Phone Number的更多相关文章

  1. 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 ...

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

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

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

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

  4. Java [leetcode 17]Letter Combinations of a Phone Number

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

  5. Leetcode 17.——Letter Combinations of a Phone Number

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

  6. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

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

  7. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

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

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

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

  9. LeetCode——17. Letter Combinations of a Phone Number

    一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...

  10. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

随机推荐

  1. Docker学习笔记4: Docker-Compose—简化复杂容器应用的利器

    本文转载自http://www.tuicool.com/articles/AnIVJn. 因Python语言,个人也没学过,不是太熟悉,这篇文章的代码格式排版不准确爆了很多错,让我走了好多坑,不过还是 ...

  2. gloox配置聊天室

    gloox配置聊天室 (金庆的专栏) gloox是XMPP协议的C++客户端库.以下代码测试创建多人聊天室(MUC), 并进行配置.参照gloox中的muc示例代码.gloox代码示例中没有聊天室的配 ...

  3. x264源代码简单分析:x264_slice_write()

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. 集合框架之Map接口

    Map是将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. Map 接口提供三种collection视图,允许以键集.值集或键-值映射关系集的形式查看某个映射的内容.映射顺序定 ...

  5. C语言与java语言中数据类型的差别总结

    在学习java的时候,看到char ch =  '男' ; 我就觉得很奇怪,char类型不是占用一个字节吗?为什么定义成一个汉字被说成是一个字符了? 原来,在C语言中,char在32位操作系统下占用1 ...

  6. Android View框架总结(五)View布局流程之Layout

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52216195 View树的Layout流程 View的Layout时序图 View布局 ...

  7. UE4读取本地XML文件

    其实这里读取XML也是利用了Tinyxml来读取xml,主要是讲Tinyxml放在UE4中,遇到的一点点坑 1.先给出Tinyxml链接:http://www.grinninglizard.com/t ...

  8. JVM的GC(概念与深入)

    深入浅出了解什么是GC: http://my.oschina.net/xianggao/blog/86985 GC策略详解: http://blog.csdn.net/winniepu/article ...

  9. javascript之事件处理

    一般事件 onclick                       鼠标点击时触发此事件 ondblclick                  鼠标双击时触发此事件 onmousedown    ...

  10. iOS中让Settings Bundle中的变化立即在App中反应出来的两种方法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了能够在Settings Bundle中的变化在进入App后 ...