LeetCode #17 Letter Combinations of a Phone Number C# Solution

原创 2016年06月01日 21:19:00

Given a digit string, return all possible letter combinations that the number could represent.
ex:
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].

回溯算法,暴力搜索= =

ugly C# Code

    public class Solution
    {
        Dictionary<char, List<char>> k = new Dictionary<char, List<char>>
        {
            { '2', new List<char>{ 'a','b','c'} },
            { '3', new List<char>{ 'd','e','f'} },
            { '4', new List<char>{ 'g','h','i'} },
            { '5', new List<char>{ 'j','k','l'} },
            { '6', new List<char>{ 'm','n','o'} },
            { '7', new List<char>{ 'p','q','r','s'} },
            { '8', new List<char>{ 't','u','v'} },
            { '9', new List<char>{ 'w','x','y','z'} }
        };
        IList<string> anss = new List<string>();
        public IList<string> LetterCombinations(string digits)
        {
            if (digits == "") return new List<string> { };
            string ans = "";
            calc(digits, 0, digits.Length, ans);
            return anss;
        }

        private string calc(string d, int p, int max, string ans)
        {
            if (p == max)
            {
                anss.Add(ans);
                return ans;
            }
            foreach (char c in k[d[p]])
            {
                calc(d, p + 1, max, ans + c);
            }
            return ans;
        }
    }

这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode17 17. Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution

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

leetcode 17. Letter Combinations of a Phone Number

题意 给定一个数字字符串,枚举各个数字对应的字母组合后的字符串(不重复)。 题解 一个串中字母的重复次数,即后面字母形成串的可能种数。 给定数字”234”,对应串“abc”、“def”、...

[Java]LeetCode17 Letter Combinations of a Phone Number

[Java]LeetCode17 Letter Combinations of a Phone Number
  • fumier
  • fumier
  • 2015年04月24日 09:47
  • 882

LeetCode OJ-17-Letter Combinations of a Phone Number

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

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS)

LeetCode OJ-17.Letter Combinations of a Phone Number(DFS解法)题目描述 Given a digit string, return all p...

LeetCode#17 Letter Combinations of a Phone Number

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

LeetCode-17.Letter Combinations of a Phone Number

https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Given a digit string, retu...

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem: Given a digit string, return all possible letter combinations that the number could re...

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译给定一个数字字符串,返回所有这些数字可以表示的字母组合。一个数字到字母的映射(就像电话按钮)如下图所示。输入:数字字符串“23” 输出:["ad", "ae", "af", "bd", "be",...
  • NoMasp
  • NoMasp
  • 2015年10月18日 10:59
  • 3373

leetcode(17):Letter Combinations of a Phone Number

原题: Given a digit string, return all possible letter combinations that the number could represent.A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode #17 Letter Combinations of a Phone Number C# Solution
举报原因:
原因补充:

(最多只允许输入30个字)