Leetcode letter combinations of a Phone number (C,c++,java,python)

Source: Internet
Author: User

Problem:

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.

Solution: Recursive enumeration can be done.
Title: Given a numeric string, pressing these numbers on the phone keypad requires all possible combinations of strings, each of which may have the following characters: map=["", "" "," abc "," Def "," Ghi "," JKL "," MnO "," PQRS "," TUV "," WXYZ "]

The idea of solving problems: with solution
Java source code (spents 253ms):
public class Solution {char[][] map={"". ToCharArray (), "". ToCharArray (), "ABC". ToCharArray (), "Def". ToCharArray () , "Ghi". ToCharArray (), "JKL". ToCharArray (), "MnO". ToCharArray (), "Pqrs". ToCharArray (), "TUV". ToCharArray (), "WXYZ"    . ToCharArray ()};    int length=0; Public list<string> lettercombinations (String digits) {list<string> res = new Arraylist<string> (        );        Length=digits.length ();        if (length==0) return res;        char[] tmp = new Char[length];        char[] newdigits = Digits.tochararray ();        Getlettercom (RES,0,NEWDIGITS,TMP);    return res;            } private void Getlettercom (list<string> res,int index,char[] digits,char[] tmp) {if (index>=length) {            String letters = new string (TMP);            Res.add (Letters);        Return        } int digit=digits[index]-' 0 ';            for (int i=0;i<map[digit].length;i++) {tmp[index]=map[digit][i]; Getlettercom (Res,index+1,diGITS,TMP); }    }}

C Language Source code (spents 1ms):
void getlettercom (char** res,char* digits,char* tmp,int index,char map[10][5],int *top) {int i,digit=digits[index]-' 0 ';    char* letters;        if (digits[index]==0) {letters= (char*) malloc (sizeof (char) *index);        tmp[index]=0;strcpy (LETTERS,TMP);        res[*top]=letters; (*top) + +;    Return        } for (i=0;map[digit][i];i++) {tmp[index]=map[digit][i];    Getlettercom (Res,digits,tmp,index+1,map,top); }}char** lettercombinations (char* digits, int* returnsize) {char map[10][5]={"", "", "abc", "Def", "Ghi", "JKL", "MnO",     "PQRS", "TUV", "WXYZ"};     char** res,*tmp;     int num=1,length=0,top=0;         while (Digits[length]) {if (digits[length]== ' 0 ' | | digits[length]== ' 1 ') continue;         else if (digits[length]== ' 7 ' | | | digits[length]== ' 9 ') num*=4;         else num*=3;     length++;     } res= (char**) malloc (sizeof (char*) *num);         if (length==0) {*returnsize=0;     return res;     } tmp= (char*) malloc (sizeof (char) *length); Getlettercom (Res,digits,tmp,0,map,&top);     *returnsize=top; return res;}

C + + source code (spents 3ms):
Class Solution {public:    vector<string> lettercombinations (string digits) {        vector<string> res;        Length=digits.size ();        if (length==0) return res;        char* tmp= (char*) malloc (sizeof (char) * (length+1));        Getlettercom (res,0,digits,tmp);        return res;    } Private:    char map[10][5]={"", "" "," abc "," Def "," Ghi "," JKL "," MnO "," PQRS "," TUV "," WXYZ "};    int top=0,length=0;    void getlettercom (vector<string>& res,int index,string digits,char* tmp) {        if (index>=length) {            tmp[index]=0;            String letters=string (TMP);            Res.push_back (Letters);            return;        }        int digit=digits[index]-' 0 ';        for (int i=0;map[digit][i];i++) {            tmp[index]=map[digit][i];            Getlettercom (res,index+1,digits,tmp);}}    ;

Python source code (spents 69ms):
Class Solution:    map=["", "", "abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"]    length=0;res=[]    # @par am {string} digits    # @return {string[]}    def lettercombinations (self, digits):        Self.length=len (digits)        self.res=[]        if Self.length==0:return self.res;        Tmp=[' For I in Range (self.length)]        self.getlettercom (0,digits,tmp)        return self.res    def getlettercom ( SELF,INDEX,DIGITS,TMP):        if index>=self.length:            letters= '. Join (TMP)            self.res.append (Letters)            return        Digit=ord (Digits[index])-ord (' 0 ') for        I in range (len (self.map[digit)):            tmp[index]= Self.map[digit][i]            self.getlettercom (index+1,digits,tmp)


Leetcode letter combinations of a Phone number (C,c++,java,python)

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.