LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
来源:互联网 发布:天思是什么软件 编辑:程序博客网 时间:2024/05/16 01:17
翻译
给定一个数字字符串,返回所有这些数字可以表示的字母组合。一个数字到字母的映射(就像电话按钮)如下图所示。输入:数字字符串“23”输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]备注:尽管以上答案是无序的,如果你想的话你的答案可以是有序的。
原图
原文
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.
代码
看样子我还是用C#顺手点,一气呵成……主要是用递归,因为不知道
public class Solution{ IList<string> list = new List<string>(); public Solution() { list.Insert(0, "abc"); list.Insert(1, "def"); list.Insert(2, "ghi"); list.Insert(3, "jkl"); list.Insert(4, "mno"); list.Insert(5, "pqrs"); list.Insert(6, "tuv"); list.Insert(7, "wxyz"); } public IList<string> LetterCombinations(string digits) { IList<string> result = new List<string>(); if (digits.Length == 0) return result; if (digits.Length == 1) { foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2)) { result.Insert(0, a.ToString()); } } int count = 0; IList<string> temp = LetterCombinations(digits.Substring(1, digits.Length - 1)); foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2)) { foreach (var rest in temp) { result.Insert(count++, a.ToString() + rest); } } return result; }}
以下是复制来的一段C++代码,坦白地说,我写不出来这样的C++代码……
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> ans; if(digits.size() == 0) return ans; int depth = digits.size(); string tmp(depth, 0); dfs(tmp, 0, depth, ans, digits); return ans; } void dfs(string &tmp, int curdep, int depth, vector<string> &ans, string &digits){ if(curdep >= depth){ ans.push_back(tmp); return ; } for(int i = 0; i < dic[digits[curdep] - '0'].size(); ++ i){ tmp[curdep] = dic[digits[curdep] - '0'][i]; dfs(tmp, curdep + 1, depth, ans, digits); } return ; }private:string dic[10] = {{""},{""},{"abc"},{"def"},{"ghi"},{"jkl"},{"mno"},{"pqrs"},{"tuv"},{"wxyz"}};};
2 0
- LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
- Letter Combinations of a Phone Number (电话号码字母组合) 【leetcode】
- 17:Letter Combinations of a Phone Number(字母组合的电话号码)
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
- 425.Letter Combinations of a Phone Number-电话号码的字母组合(中等题)
- Letter Combinations of a Phone Number 电话号码的组合
- 【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】
- Leetcode刷题记——17. Letter Combinations of a Phone Number(电话号码的数字组合)
- LeetCode 17 Letter Combinations of a Phone Number 数字转为字母组合
- Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节
- leetcode——Letter Combinations of a Phone Number 手机按键字母组合(AC)
- 17. Letter Combinations of a Phone Number(根据手机按键求字母的组合)
- Letter Combinations of a Phone Number || 电话号码对应的单词
- leetCode 17.Letter Combinations of a Phone Number(电话数字对应的字母组合) 解题思路和方法
- LeetCode | Letter Combinations of a Phone Number(号码的字符串组合)
- LeetCode Letter Combinations of a Phone Number 经典组合问题
- LeetCode (17)Letter Combinations of a Phone Number
- LeetCode (17)Letter Combinations of a Phone Number
- 三个线程循环打印ABC。。。。
- java面试题2(115个Java面试题和答案)
- 关于PHP实现迭代器和迭代
- Java的强制类型转换与隐式类型转换
- 黑马程序员——Java对象
- LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
- Masonry介绍与使用实践:快速上手Autolayout
- hdu5504 GT and sequence(BestCoder Round #60 )
- hdu5505(好题)
- 两个线程按特定时间顺序交替执行
- softMax(2)---predict实现
- hdu1251 统计难题 字典树
- 基站定位属性
- 网络获取json解析