translation
给定一个数字字符串,返回所有这些数字可以表示的字母组合。一个数字到字母的映射(就像电话按钮)如所示。输入:数字字符串“23”输出:["ad""ae""af""bd""be""bf""cd""ce""cf"]备注:尽管以上答案是无序的,如果你想的话你的答案可以是有序的。
Original
Original
Given a digitstring,returnAll possible letter combinations that the Numbercould represent. A Mapping ofDigit toLetters (Just Like on theTelephone buttons) is given below. Input:digitstring "All"Output: ["AD","AE","AF","BD","Be","BF","CD","CE","CF"]. Note:although the aboveAnswer is inchlexicographical order, your answer could beinchAny order you want.
Code
It seems that I still use C # handy point, one go ... Mostly with recursion, because I don't know < Span class= "Mrow" id= "mathjax-span-67" > d i g i t s The length of the end is how much, impossible to write countless < Span class= "Mrow" id= "mathjax-span-75" > f o r e a c h To judge.
Public classsolution{ilist<string> list =Newlist<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"); } Publicilist<string>lettercombinations(stringdigits) {ilist<string> result =Newlist<string> ();if(digits. Length = =0)returnResultif(digits. Length = =1) {foreach(varAinchList. ElementAt (int. Parse (digits[0]. ToString ())-2) {result. Insert (0, a.tostring ()); } }intCount =0; ilist<string> temp = lettercombinations (digits. Substring (1, digits. Length-1));foreach(varAinchList. ElementAt (int. Parse (digits[0]. ToString ())-2)) {foreach(varRestinchTemp) {result. Insert (count++, a.tostring () + rest); } }returnResult }}
Here is a copy of the C + + code, frankly, I can not write such C + + code ...
classSolution { Public: vector<string>Lettercombinations (stringDigits) { vector<string>Ansif(digits.size () = =0)returnAnsintdepth = Digits.size ();stringTMP (Depth,0); DFS (TMP,0, depth, ans, digits);returnAns }voidDfsstring&tmp,intCURDEP,intDepth vector<string>&ans,string&digits) {if(CURDEP >= depth) {ans.push_back (TMP);return; } for(inti =0; I < DIC[DIGITS[CURDEP]-' 0 '].size (); + + i) {TMP[CURDEP] = Dic[digits[curdep]-' 0 '][i]; DFS (TMP, CURDEP +1, depth, ans, digits); }return; }Private:stringdic[Ten] = {{""},{""},{"ABC"},{"Def"},{"Ghi"},{"JKL"},{"MnO"},{"PQRS"},{"TUV"},{"WXYZ"}};};
Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp
Leetcode letter combinations of a phone number (alphanumeric combination of phone numbers)