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 ...
随机推荐
- IDE Fix Pack 6.4.2 released (bugfix release)
IDE Fix Pack 6.4.2 addresses two bugs. It fixes an issue with the TCustomListBox.ResetContent patch ...
- c#的默认访问权限
1.命名空间下的元素的默认访问修饰符 public : 同一程序集的其他任何代码或引用该程序集的其他程序集都可以访问该类型或成员. internal : 同一程序集中的任何代码都可以访问该类型或成员, ...
- python day21 ——面向对像-反射 getattr,内置方法
一.反射:用字符串数据类型的变量名来访问这个变量的值 上代码^_^ # class Student: # ROLE = 'STUDENT' # @classmethod # def check_cou ...
- WEB学习笔记1-综述
WEB前端基本技术:HTML.CSS.JavaScript 概念: 从职责上讲,Web前端开发要涉及网站开发的方方面面,从前端UI到和后端的数据交互都属于前端开发的范畴.Web前端开发是兼具艺术气息和 ...
- oracle 如何查询/修改dmp文件的字符集
1.如何查询dmp文件的字符集 用oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用Ul ...
- CMake 构建项目教程-简介
CMake 构建项目教程-简介 Linux 平台构建项目,选择了CLion作为C++的IDE,而CLion默认就是使用CMake构建项目,所以这里记录了CMake在构建项目过程的一些小知识. 1. 项 ...
- Linux 挂载windows目录
1.默认情况下,Linux服务器会装有samba-client,但是没有装samba-server.但是访问Windows系统共享,安装有samba-client就可以了. [root@test ~] ...
- python学习之路06——字符串
字符串 1.概念 字符串就是由若干个字符组成的有限序列 字符:字母,数字,特殊符号,中文 表示形式:采用的单引号或者双引号 注意:字符串属于不可变实体 2.创建字符串 str1 = "hel ...
- centos安装redis步骤
1.官网或wget下载redis-4.0.9.tar.gz: cd /home/tar wget http://download.redis.io/releases/redis-4.0.9.tar.g ...
- django的静态文件的引入
django的静态文件的引入 1.路径配置 在templates文件夹的同级目录下新建static文件夹 在setting里面写上STATICFILES_DIRS = [os.path.join(BA ...