题意:

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.(Medium)

分析:

很好的练习搜索的题目,可以用BFS和回溯法(DFS)两种方式来做一下练习。

自己写的时候没太想明白回溯的写法(参数,返回值等等),写的是BFS的解法,DFS参考的讨论区。

代码1:

 class Solution {
public:
vector<string> letterCombinations(string digits) {
string digMap[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> v;
if (digits.size() == ) {
return v;
}
queue<string> q;
for (int i = ; i < digMap[digits[] - ''].size(); ++i) {
q.push(string(,digMap[digits[] - ''][i] ));
}
for (int i = ; i < digits.size(); ++i) {
int l = q.size();
for (int j = ; j < l; ++j) {
string temp1 = q.front();
q.pop();
for (int k = ; k < digMap[digits[i] - ''].size(); ++k) {
string temp2 = temp1 + digMap[digits[i] - ''][k];
q.push(temp2);
}
}
}
while (!q.empty()) {
v.push_back(q.front());
q.pop();
}
return v;
}
};

回溯法:

 class Solution {
private:
string digMap[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void dfs(vector<string>& v, string& tempStr, int index, string& digits) {
if (index == digits.size()) {
v.push_back(tempStr);
return;
}
for (int i = ; i < digMap[digits[index] - ''].size(); ++i) {
//tempStr += digMap[digits[index] - '0'][i];
tempStr.push_back(digMap[digits[index] - ''][i]); //string的push_back和pop_back以前没用过
dfs(v,tempStr,index + ,digits);
tempStr.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
if (digits.size() == ) {
return result;
}
string tempStr;
dfs(result,tempStr,,digits);
return result;
}
};
 

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

  1. 算法练习--LeetCode--17. Letter Combinations of a Phone Number

    Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...

  2. Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合

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

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

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

  4. 69. Letter Combinations of a Phone Number

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

  5. 【leetcode】Letter Combinations of a Phone Number

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

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

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

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

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

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

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

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

随机推荐

  1. 2款好用的Web在线编辑器

    1.CKEditor FCKEditor 现在已经重新开发,并改名为 CKEditor. CKeditor是一个专门使用在网页上,开放源代码,高度可定制,跨平台的所见即所得文字编辑器,兼容于绝大部分的 ...

  2. Jquery 等待ajax返回数据loading控件ShowLoading组件

    1.意义 开发项目中,前台的页面要发请求到服务器,服务器响应请求返回数据到前台,这段时间,有可能因为返回的数据量较大导致前台页面出现短暂性的等待,此时如果用户因不知情而乱点击有可能造成逻辑混乱,所以此 ...

  3. QueryInterface

    QueryInterface IUnknown *p2; hr = pInnerUnknown->QueryInterface(vGUID2, (void**)&p2); IUnknow ...

  4. 转】MyEclipse使用总结——设置MyEclipse开发项目时使用的JDK

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3927677.html 感谢! 安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyE ...

  5. HTML5每日一练之视频标签的应用

    与音频一样,在过去,我们如果想在Web上播放视频,也是都是通过Flash来播放,同样并不是所有的浏览器都安装了Flash播放器插件,而现在我们在HTML5中,就能完全脱离Flash或其他的插件来播放视 ...

  6. What's Exposure?

    [What's Exposure?] ISO:即相机的感光度.ISO数值的大小是DC对光线反应的敏感程度测量值,通常以ISO数值表示,数值越大表示对光线的敏感性越强,数值越小表示越弱,是控制曝光量的一 ...

  7. tomcat 粗略笔记

    GlobalNamingResources 存在于server.xml中,定义全局公共数据源,如果host中有大量引用相同的数据源,那么可以都配在这里 <GlobalNamingResource ...

  8. Hibernate资源

    正在学马士兵Hibernate的同学来看这里,这里提供了他视频里需要的JAR包,请尽情下载,给好评喔. 一.Hibernate 3.3.2 核心JAR包 http://pan.baidu.com/s/ ...

  9. js关闭当前页面(窗口)的几种方式总结

    1. 不带任何提示关闭窗口的js代码 <a href="javascript:window.opener=null;window.open('','_self');window.clo ...

  10. HDU3874Necklace(树状数组+离线操作)

    此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...