"Leetcode-Interview algorithm classic-java implementation" "017-letter combinations of a phone number (word combination on phone numbers)"

Source: Internet
Author: User

"017-letter combinations of a phone number (word combination on phone numbers)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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:"23"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.

Main Topic

Given a string of numbers, returns all combinations of all characters on the number, as shown in the number-to-character mapping.
  Note: Although the above results are arranged in character order, you can return the results in any order.

Thinking of solving problems

Use an array to hold the mapping of numbers and words, and according to the input of the number string, find the corresponding characters and combine the results.

Code Implementation
 Public classSolution {Privatestring[] Map = {"ABC","Def","Ghi","JKL","MnO","PQRS","TUV","WXYZ",    };Privatelist<string> result;//Store final results    Private Char[] chars;//Save the result of removing 0, 1 characters    Private Char[] curresult;//Store intermediate results    Private intEnd =0;The first unused position in the//character array    Private intHandle =0;//The number of characters that are currently being processed     PublicList<string>lettercombinations(String digits) {result =NewLinkedlist<> ();if(Digits! =NULL&& digits.length () >0) {chars = Digits.tochararray ();//string processing, minus 0 and 1            //Find the first 0 or 1 position             while(End < Digits.length () && chars[end]! =' 0 '&& Chars[end]! =' 1 ') {end++; } handle = end +1; while(Handle < Chars.length) {if(Chars[handle]! =' 0 '&& Chars[handle]! =' 1 ') {Chars[end] = Chars[handle];                end++;            } handle++; } Curresult =New Char[end];//While end, end is the length of a valid characterHandle =0;//point to the position of the first valid characterLettercombinations (); }returnResult }Private void lettercombinations() {if(Handle >= end) {Result.add (NewString (Curresult)); }Else{intnum = Chars[handle]-' 2 '; for(inti =0; I < map[num].length ();                i++) {Curresult[handle] = Map[num].charat (i);                handle++;                Lettercombinations ();            handle--; }        }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46980259"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java implementation" "017-letter combinations of a phone number (word combination on phone numbers)"

Related Article

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.