LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem:

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

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

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Solution:

递归枚举即可。

题目大意:

给定一个数字字符串,在手机按键上按下这些数字,要求得出所有可能的字符串组合,每个字可能会有的字符有以下这些:

map=[" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]

解题思路:

同Solution

Java源代码(用时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语言源代码(用时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++源代码(用时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源代码(用时69ms):

class Solution:
    map=[" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
    length=0;res=[]
    # @param {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)
时间: 2024-12-29 09:55:05

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)的相关文章

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 017.Letter_Combinations_of_a_Phone_Number (Medium) 链接: 题目:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ 代码(github):https://github.co

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Ou

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given

Leetcode 17 Letter Combinations of a Phone Number - DFS思想

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode #17 Letter Combinations of a Phone Number (M)

[Problem] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

LeetCode 17 Letter Combinations of a Phone Number 数字转为字母组合

题目: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

Leetcode 17.——Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap<Character, String> map = new HashMap<>(); map.put('0', "0"); map.put('1', "1"); map.put('2', "abc"); map.put('3', &