leetcode17
回溯法,深度优先遍历(DFS)
public class Solution
{
int[] x;
int N;
string DIGITS;
Dictionary<char, List<string>> dic = new Dictionary<char, List<string>>();
List<string> LIST = new List<string>();
private void BackTrack(int t)
{
if (t >= N)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < N; i++)
{
var dkey = DIGITS[i];
var temp = dic[dkey][x[i]];
sb.Append(temp);
}
LIST.Add(sb.ToString());
return;
}
for (int i = ; i < dic[DIGITS[t]].Count; i++)
{
x[t] = i;
BackTrack(t + );
}
}
private void Init()
{
dic.Add('', new List<string> { "a", "b", "c" });
dic.Add('', new List<string> { "d", "e", "f" });
dic.Add('', new List<string> { "g", "h", "i" });
dic.Add('', new List<string> { "j", "k", "l" });
dic.Add('', new List<string> { "m", "n", "o" });
dic.Add('', new List<string> { "p", "q", "r", "s" });
dic.Add('', new List<string> { "t", "u", "v" });
dic.Add('', new List<string> { "w", "x", "y", "z" });
}
public IList<string> LetterCombinations(string digits)
{
var n = digits.Length;
if (n > )
{
Init();
N = n;
x = new int[n];
DIGITS = digits;
BackTrack();
}
return LIST;
}
}
提供一种更直接的思路:
public class Solution {
public IList<string> LetterCombinations(string digits) {
var combinations = new List<string>();
if(string.IsNullOrEmpty(digits))
return combinations;
combinations.Add("");
foreach(char digit in digits) {
var next = new List<string>();
foreach(char letter in GetLetters(digit)) {
foreach(string combo in combinations) {
next.Add(combo + letter);
}
}
combinations = next;
}
return combinations;
}
public char[] GetLetters(char digit) {
switch (digit) {
case '':
return new char[] { 'a', 'b', 'c' };
case '':
return new char[] { 'd', 'e', 'f' };
case '':
return new char[] { 'g', 'h', 'i' };
case '':
return new char[] { 'j', 'k', 'l' };
case '':
return new char[] { 'm', 'n', 'o' };
case '':
return new char[] { 'p', 'q', 'r', 's' };
case '':
return new char[] { 't', 'u', 'v' };
case '':
return new char[] { 'w', 'x', 'y', 'z' };
default:
return new char[];
}
}
}
补充一个python的实现:
class Solution:
def backTrack(self,digits,dic,temp,res,i):
if i == len(digits):
res.append(''.join(temp[:]))
return key = digits[i]
li = dic[key]
for j in range(len(li)):
temp.append(li[j])
self.backTrack(digits,dic,temp,res,i+)
temp.pop(-) def letterCombinations(self, digits: 'str') -> 'List[str]':
n = len(digits)
if n == :
return []
res = []
dic = {'':['a','b','c'],'':['d','e','f'],'':['g','h','i'],
'':['j','k','l'],'':['m','n','o'],'':['p','q','r','s'],'':['t','u','v'],'':['w','x','y','z']}
self.backTrack(digits,dic,[],res,) return res
思路:回溯法。
回溯函数的参数含义:
digits:原字符串,dic:按键字典,temp:字母组合的临时存储,res:最终结果的列表,i:digits的索引。
leetcode17的更多相关文章
- Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 .注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- 算法练习--LeetCode--17. Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...
- 【1】【leetcode-17】电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[" ...
- string+DFS leetcode-17.电话号码下的字母组合
题面 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- Java实现LeetCode17. 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
随机推荐
- pip 安装 nexmo
pip install nexmo报错 是因为缺少 libffi-devel 需要 yum install libffi-devel 然后再执行 pip install nexmo 即可成功
- Jsの练习-数组常用方法
1. join() 方法: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- 运维grep语法
grep的语法和用法 grep命令的格式: grep [options] PATTERN [FILE] 其中:1,pattern是用正则表达式书写的模式.2,FILE是要查找的文件,可以是用 ...
- 1.3 解决pip使用异常问题
1.3 解决pip使用异常问题 1.3.1 pip出现异常有一小部分童鞋在打开cmd输入pip后出现下面情况:Didnot provide a command Did not provide a co ...
- 项目三(2)——person与学生
编程实现Person类,学生类的设计及其继承关系 (同第六章课后第三题) class Person{ private String name; private String addr; private ...
- 基于redis的延迟消息队列设计(转)
需求背景 用户下订单成功之后隔20分钟给用户发送上门服务通知短信 订单完成一个小时之后通知用户对上门服务进行评价 业务执行失败之后隔10分钟重试一次 类似的场景比较多 简单的处理方式就是使用定时任务 ...
- python中calendar模块的常用方法
>>> import calendar >>> calendar.isleap(2000) #判断是否是闰年 True >>> calendar. ...
- SQL-视图-004
什么是视图? 优点:视图是保存在数据库中的SELECT查询,可以简化查询操作,视图可以从多个表中提取数据,并以单个表的形式显示结果,这样就可以针对多个表的查询转化为对一个表的查询. 映射:视图可以理解 ...
- JavaStudy——Java之自动拆箱与自动装箱
java基本类型介绍 java中,基本数据类型一共有8种,详细信息如下表: 类型 大小 范围 默认值 byte 8 -128 - 127 0 short 16 -32768 - 32768 0 int ...
- Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
在windows下设置字节对齐大小的方式,目前我了解有三种: 1. 在编译程序时候的编译选项 /Zp[n],如 cl /Zp4 表示对齐大小是4字节: 2. 预处理命令 #pragma pack ...