[Java] LeetCode17 letter combinations of a Phone number

Source: Internet
Author: User

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"].
Test instructions: A string consisting of numbers that outputs all the combinations that can appear, according to the string corresponding to the number on the phone. The order in the string is from small to large
Problem Solving ideas: This is the dynamic accumulation of the program. If I say, "23", I first find the combination of 2, and then the combination of 2 and 3 in the corresponding all the characters one by one combination, this is obvious. What if it's "234"? Before that, all the combinations of 23 have been found, and then, as long as the 23 combinations and 4 of each character one by one are combined. The same analogy, the recursive principle.
public static list<string> lettercombinations (String digits) {List<string> list=new arraylist<string> ();List<string> res=new arraylist<string> ();int Len=digits.length ();if (len==0) return res;Char ch;String Tmp=null;for (int i=0;i<len;i++){Ch=digits.charat (i);if (ch!= ' 0 ' &&ch!= ' 1 '){Tmp=digit2string (CH);List.add (TMP);}}Len=list.size ();for (int i=0;i<len;i++){Res=mergelistandstring (Res,list.get (i));}return res;}Static list<string> mergelistandstring (list<string> list,string str){if (str==null) return list;List<string> restmp=new arraylist<string> ();if (List.size () ==0){for (int i=0;i<str.length (); i++)List.add (string.valueof (Str.charat (i)));return list;}else{for (int j=0;j<str.length (); j + +){for (int k=0;k<list.size (); k++){Restmp.add (Str.charat (j) +list.get (k));}}return restmp;}}Static String digit2string (char ch){String Str=null;Switch (CH){Case ' 2 ': str= "abc";Case ' 3 ': str= "def";Case ' 4 ': str= "Ghi";Case ' 5 ': str= "JKL";Case ' 6 ': str= "MNO";Case ' 7 ': str= "PQRS";Case ' 8 ': str= "TUV";Case ' 9 ': str= "WXYZ";Default:str=null;break;}return str;}
This is the first version of the code, though it passed. But the code is very long. Next, see if there is any way to streamline.
public static list<string> lettercombinations (String digits) {string[] digits2string={"", "", "abc", "D EF "," Ghi "," JKL "," MnO "," PQRS "," TUV "," WXYZ "};List<string> res=new arraylist<string> ();int Len=digits.length ();if (len==0) return res;Char ch;String Tmp=null;for (int i=0;i<len;i++){Ch=digits.charat (i);if (ch<= ' 9 ' &&ch> ' 1 '){tmp=digits2string[ch-' 0 '];Res=mergelistandstring (RES,TMP);}}return res;}Static list<string> mergelistandstring (list<string> list,string str){if (str==null) return list;List<string> restmp=new arraylist<string> ();if (List.size () ==0){for (int i=0;i<str.length (); i++)List.add (string.valueof (Str.charat (i)));return list;}else{for (int j=0;j<list.size (); j + +){for (int k=0;k<str.length (); k++){Restmp.add (List.get (j) +str.charat (k));}}return restmp;}}

[Java] LeetCode17 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.