Lintcode Medium title: letter combinations of a phone number in alphabetical combination

Source: Internet
Author: User

Topic

letter combinations for phone numbers

Give a numeric string, each number represents a letter, and return all possible combinations of letters.

Phone keypad, which represents the letters each number can represent.

Sample Example

Given"23"

Return["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

Note

The answers above are output in the order of the dictionaries, but you can choose the output sequence you like when you do the subject.

Solving

Unable to understand the answer

Backtracking means incomprehensible.

 Public classSolution {/**     * @paramdigits A Digital String *@returnAll posible letter combinations*/     PublicArraylist<string>lettercombinations (String digits) {//Write Your code herehashmap<integer,string> map =NewHashmap<integer,string>(); Map.put (0, ""); Map.put (1, ""); Map.put (2, "ABC"); Map.put (3, "Def"); Map.put (4, "Ghi"); Map.put (5, "JKL"); Map.put (6, "MnO"); Map.put (7, "PQRS"); Map.put (8, "TUV"); Map.put (9, "WXYZ"); ArrayList<String> result =NewArraylist<string>(); if(Digits = =NULL|| Digits.length () ==0 )            returnresult; ArrayList<Character> tmp =NewArraylist<character>();        Numtostring (DIGITS,TMP,RESULT,MAP); returnresult; }     Public voidNumtostring (String digits,arraylist<character> tmp,arraylist<string> result,hashmap<integer,string >map) {        if(Digits.length () ==0){            Char[] arr =New Char[Tmp.size ()];  for(intI=0;i< tmp.size (); i++) {Arr[i]=Tmp.get (i);            } result.add (String.valueof (arr)); return; } Integer Curr= Integer.valueof (digits.substring (0,1)); String Letters=Map.get (Curr);  for(inti = 0;i< letters.length (); i++) {Tmp.add (Letters.charat (i)); Numtostring (Digits.substring (1), Tmp,result,map); Tmp.remove (Tmp.size ()-1); }    }}
View Code

Lintcode Medium title: letter combinations of a phone number in alphabetical combination

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.