Leetcode_letter combinations of a Phone number

Source: Internet
Author: User

Describe:

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

A mapping of Digit to letters (just as on the telephone buttons) is given below.

Input: Digit string "Output": ["Ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].

Note:
Although the above answer is in lexicographical order, your answer could are in any order you want.

Ideas:

The effect of this topic to achieve is to produce a series of generalized ordered string sequence, originally want to know digits string length, can engage an n-layer loop, but the length of digits is unknown. Think for a long time, think of the implementation of this topic with recursion to achieve the function, each number to the number of characters to be represented by the number of numbers, return to the upper level of recursion, and then in the upper level of the cycle level count on the 1 can be added.

When the length of the character equals the length of the digits, it indicates that a valid set of strings has been generated, stored, then the last character is deleted, and the loop continues ....

Finally, when index<0, the entire cycle is over, over! However, in the implementation of the process, recursive understanding is more complex, how to end, this judgment condition requires special attention, if only a simple return may not achieve the desired effect.

In short, this topic for two hours, feel a bit messy.

Code:

Boolean flag=true;int digitslength=0;int arr[]; List<string>list=new arraylist<string> (); StringBuilder sbuilder=new StringBuilder (); Map<integer, String>map=new Hashmap<integer, string> ();p ublic list<string> letterCombinations ( String digits) {if (digits==null| |        Digits.length () ==0) return list;        Digitslength=digits.length ();        Arr=new Int[digitslength];        int Digitsarr[]=new int[]{2,3,4,5,6,7,8,9};        String strarr[]=new string[]{"abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"};        int len=digitsarr.length;        Arrays.fill (arr, 0);        for (int i=0;i<len;i++) map.put (Digitsarr[i], strarr[i]);//numeric corresponding character combinations (digits,0);    return list; }public void combinations (String Digits,int index) {if (!flag)//flag false when completely ended Return;if (index<0) {flag=false;// Index<0 end return completely;} int Number=digits.charat (index)-' 0 '; String string=map.get (number);//Gets the character of the digit int len=string.length (); int uplevelindex=0;if (index<digitSLENGTH-1)//has not reached the length of the valid character {if (Arr[index]==len)//To reach the maximum value of the number corresponding character {arr[index]=0;uplevelindex=sbuilder.length () -1;// Before the previous layer of the loop, the first layer of the number corresponding to the character is deleted if (uplevelindex>=0) {Sbuilder.deletecharat (uplevelindex); combinations (digits, INDEX-1);} else {flag=false;//arrives at the front return;}} Else{sbuilder.append (String.charat (Arr[index)); Arr[index]++;combinations (digits,index+1);}} else//reaches the length of the valid string {for (int i=0;i<len;i++) {sbuilder.append (String.charat (i)); List.add (sbuilder.tostring ());// Save the result Sbuilder.deletecharat (Sbuilder.length ()-1);//Delete the last character, continue the Loop}uplevelindex=sbuilder.length () -1;//before the previous layer of the loop, First, delete the character corresponding to the number above the IF (uplevelindex>=0) {Sbuilder.deletecharat (uplevelindex); combinations (digits,index-1);} else {flag=false;//arrives at the front return;}}}

Results:

Leetcode_letter combinations of a Phone number

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.