回溯法,深度优先遍历(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的更多相关文章

  1. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  2. LeetCode17 Letter Combinations of a Phone Number

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

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

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  4. LeetCode17.电话号码的字母组合 JavaScript

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

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

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

  6. 【1】【leetcode-17】电话号码的字母组合

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

  7. string+DFS leetcode-17.电话号码下的字母组合

    题面 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...

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

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

  9. Java实现LeetCode17. 电话号码的字母组合

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

随机推荐

  1. 马凯军201771010116《面向对象程序设计(java)》第七周学习总结

    理论与知识部分 多态性:概念:指在程序中同一符号在不同的情况下具有不同的解释.超类中定义的域或方法,被子类继承之后,可以具有不同的数据类型或表现出不同的行为.这使得同一域或方法在超类及各个子类中具有不 ...

  2. python3+requests+unittest:接口自动化测试(一)

    转载请表明出处:https://www.cnblogs.com/shapeL/p/9179484.html 简单介绍框架的实现逻辑,参考代码的git地址: https://github.com/zha ...

  3. VS使用Nuget教程详解 Visual studio 安装第三方的组件库

    首先说明Nuget是什么呢?它的官方是:https://www.nuget.org/ 官方主页的介绍如下: What is NuGet? NuGet is the package manager fo ...

  4. zabbix3.4安转

    其实很简单  在官网上有安装文档,我只不过是将官网上的拿了下来 安装apache和php 数据库mariadb(我的系统是centos7的所以安装mariadb) 开始zabbix的安装 本次实验 系 ...

  5. Linux_x86下NX与ASLR绕过技术(续)

    四.Stack Canaries 首先看一下Stack Canaries演进历史: Stack Guard 是第一个使用 Canaries 探测的堆栈保护实现,它于 1997 年作为 GCC 的一个扩 ...

  6. supervisord.conf

    ; Sample supervisor config file.;; For more information on the config file, please see:; http://supe ...

  7. Python:从入门到实践--第十章--文件和异常--练习

    #.python学习笔记:在文本编辑器中新创建一个文件,写几句话老总结你至此学到的python知识 #其中‘In Python you can’ 打头.将这个文件命名为learning_python. ...

  8. e充电加密破解

    def encrypt(self,stationId ): keys = 'F29E0E39-98E4-F4CC318443' encrypt_obj = pyDes.triple_des(keys, ...

  9. python中时间、日期、时间戳的转换

    1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字符类型的时间 tss1 ...

  10. php正则判断是否同时有数字和字母

    1.php正则判断是否同时有数字和字母$subject = 'qq2';var_dump(preg_match('/^(?![0-9]+$)(?![a-zA-Z]+$)/', $subject)); ...