Letter Combinations of a Phone Number

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.

思路: hash && dfs.
void dfs(vector<string>& out, string ans, int curDepth, string& digits, string map[10] ){
if(curDepth == digits.length()){
out.push_back(ans);
return;
}
for(int i = 0; i < map[digits[curDepth]-'0'].length(); ++i){
ans.push_back(map[digits[curDepth]-'0'][i]);
dfs(out, ans, curDepth+1, digits, map);
ans.pop_back();
}
} class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> out;
string ans;
string map[10] = {"", "", "abc","def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
dfs(out, ans, 0, digits, map);
return out;
}
};

69. Letter Combinations of a Phone Number的更多相关文章

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  2. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

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

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

  4. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  5. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

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

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

  7. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  8. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  9. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

随机推荐

  1. 排序小结(C++版)

    一.快速排序 #include <iostream> using namespace std; int adjust(int a[],int start,int end) { int i, ...

  2. asp.net导出word(word2007)

    1.只能导出成word2007格式(.docx),可直接导出到客户端 2.服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片) 3.需要一个国外的DocX.dll插件 4.需要添加引 ...

  3. UIImagePickerController 获取相片视频

    1.UIImagePickerController的静态方法: imagepicker = [[UIImagePickerController alloc]init];    //UIImagePic ...

  4. 在MAC OS X上如何启用crontab?

    project: blog target: how-to-enable-crontab-on-osx.md date: 2015-12-16 status: publish tags: - OS X ...

  5. namenode需要升级

    java.io.IOException:File system image contains an old layout version -18.An upgrade to version -41 i ...

  6. Roman to Integer -- LeetCode 13

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  7. Maven项目的核心pom.xml解释(转)

    文章转自http://blog.sina.com.cn/s/blog_534f69a001010lpv.html maven项目的核心是pom.xml.POM(Project Object Model ...

  8. jsp连接mysql数据库

    1.新建一个Java web项目. 2.导入mysql驱动包.(这个跟上一篇写的Java连接mysql类似) 3.编写测试代码 <%@ page contentType="text/h ...

  9. c语言求平面上2个坐标点的直线距离、求俩坐标直线距离作为半径的圆的面积、递归、菲波那次数列、explode

    #include <stdio.h> #include <math.h> #include <string.h> char explode( char * str ...

  10. ASP.Net Session, Cookie, Cache的区别

    Session—管理用户会话状态 什么是状态管理? 指对同一页或不同页的多个请求维护状态和页信息的过程 为什么要进行状态管理? Web页是无状态的,不保存任何用户请求信息,而且到服务器的每一往返过程都 ...