一天一道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. 初识Spark2.0之Spark SQL

    内存计算平台spark在今年6月份的时候正式发布了spark2.0,相比上一版本的spark1.6版本,在内存优化,数据组织,流计算等方面都做出了较大的改变,同时更加注重基于DataFrame数据组织 ...

  2. 安卓框架——SlidingMenu使用技巧

    SlidingMenu的一些常用属性 原文转载http://blog.csdn.net/zwl5670/article/details/48274109 [java] view plain copy ...

  3. 无需密码通过scp命令+key的方式实现文件传输

    如果觉得scp每次都要输入密码很麻烦, 那么这是解决方案.假设你平时在windows上开发,用户名是xiang, 你有一台Ubuntu服务器wdksw.com, 用户名是root.现在你准备上传一些文 ...

  4. Android开发指南--0 总览

    无意间发现一个网站,主打IOS方面的教程,然而作为一个Android开发者,我就找了下网站里有没有Android的教程,还真有,这里就翻译一下. 翻译目标教程:https://www.raywende ...

  5. Swift基础之守卫语句guard

    本篇文章翻译自:http://ericcerney.com/swift-guard-statement/原作者:ecerney该语法为swift2.0之后添加的新特性 最开始在Apple的Platfo ...

  6. The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required

    在Android 6.0(API 23)中,Google已经移除了移除了Apache HttpClient相关的类.HttpResponse类.缺失jar包使用HttpResponse等会报错: Th ...

  7. GC垃圾回收算法

    什么是GC垃圾回收呢.日常生活中我们去餐厅吃饭吃完饭,吃完饭走了餐具不用管,服务员在把餐具拿走,这是一种方式,服务员怎么知道他要来把餐具拿走呢,因为你走了,这个位置空了.服务员什么时候拿走餐具很重要, ...

  8. 【Netty源码分析】Reactor线程模型

    1. 背景 1.1. Java线程模型的演进 1.1.1. 单线程 时间回到十几年前,那时主流的CPU都还是单核(除了商用高性能的小机),CPU的核心频率是机器最重要的指标之一. 在Java领域当时比 ...

  9. 2.7、Android Studio使用翻译编辑器本地化UI

    如果你的应用支持多语言,你需要合理的管理你的翻译的string资源.Android Studio 提供了翻译编辑器来使查看和管理翻译的资源更加容易. 关于翻译编辑器 翻译后的资源在你的项目里保存在不同 ...

  10. UI设计--->全心全意为人民服务的宗旨---->注重客户体验--->软件持久的生命力

    UI即User Interface(用户界面)的简称.UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性有品味,还要让软件的操作变得舒适简单.自由,充分体现 ...