【Leetcode】【Medium】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.
解题思路:
需要穷举不同中排列可能,典型需要回溯的思想;
可以选择递归或非递归的形式实现回溯;
递归:
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> lst;
string ans;
if (digits == "")
return lst;
Backtracking(lst, ans, digits, );
return lst;
} void Backtracking(vector<string> &lst, string ans, string digits, int idx) {
if (idx == digits.size()) {
lst.push_back(ans);
return;
}
string cur_chars = d2l[digits[idx] - ''];
for (int i = ; i < cur_chars.size(); ++i)
Backtracking(lst, ans + cur_chars[i], digits, idx + );
} private:
vector<string> d2l = {
" ", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"
};
};
非递归:
【Leetcode】【Medium】Letter Combinations of a Phone Number的更多相关文章
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- 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 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- ActivityManager的代理模式
从官方文档的介绍可以看到ActivityManager的作用: 是与系统所有正在运行着的Acitivity进行交互,对系统所有运行中的Activity相关信息(Task,Memory,Service, ...
- 每一次要fix的pr
1.TODO一定要加自己名字 2.写代码考虑别人的阅读,比如event这样很general的名字不要用,所以不用from sqlalchemy import event, 要用import sqlal ...
- git 学习之基本操作
之前的帖子已经讲述了什么是 Git 的仓库,并且添加了文件到 Git 的仓库,这里我们来学习下一些简单的操作. status 和 diff 之前我们已经提交了了一个 testFile.txt 的文件 ...
- Linux 线程实现机制分析--转
http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 一.基础知识:线程和进程 按照教科书上的定义,进程是资源管理的最小单位,线程是程 ...
- Windows开启telnet命令
1.点击开始 → 运行 → 输入telnet,回车. 2.点击启用或关闭Windows功能 3.找到Telnet客户端,勾选,点击确认 4.搞定,测试一下 打开CMD,在出来的DOS界面里,输入tel ...
- 在WPF中自定义控件
一, 不一定需要自定义控件在使用WPF以前,动辄使用自定义控件几乎成了惯性思维,比如需要一个带图片的按钮,但在WPF中此类任务却不需要如此大费周章,因为控件可以嵌套使用以及可以为控件外观打造一套新的样 ...
- Silverlight & Blend动画设计系列十一:沿路径动画(Animation Along a Path)
Silverlight 提供一个好的动画基础,但缺少一种方便的方法沿任意几何路径对象进行动画处理.在Windows Presentation Foundation中提供了动画处理类DoubleAnim ...
- spring整合struts2和hibernate
1.spring 1.1 jar包 1.2 spring.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- 阿里Java开发电话面试经历--惨败
近期准备跳槽,想试试知名大企业--阿里.经过boss直聘上一些内部人员的内推,有幸获得了一次电话面试的机会.(虽然在面试开始之前就大概知道结果是如何,但是也总得试试自己个有多水,哈哈哈...) 跟大家 ...
- Angular4 step by step.4
1.官方的模拟远程调用API接口没整出来,干脆自己使用 最新版本 .netcore2.1.0 preview 作为请求地址 2.直接上图懒得沾代码了,等完善后再开放所有源码: 3.使用了Chole.O ...