Leetcode letter combinations of a phone number (alphanumeric combination of phone numbers)

Source: Internet
Author: User


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.

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.