LeetCode 题解(77): 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", "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.


题解:

用求Permutation的方法解决。

C++版:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> results;
        if(!digits.length())
            return results;
        string str[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        vector<string> phone(str, str + 10);
        for(int i = 0; i < phone.size(); i++)
            cout << phone[i] << endl;
        
        for(int i = 0; i < phone[digits[0]-'0'].length(); i++) {
            string current = "";
            current += phone[digits[0]-'0'][i];
            helper(results, digits.substr(1, digits.length()-1), current, phone);
        }
        return results;
    }
    
    void helper(vector<string> & results, string s, string current, vector<string> & phone) {
        if(!s.length()) {
            results.push_back(current);
            return;
        }
        for(int i = 0; i < phone[s[0]-'0'].length(); i++) {
            string local = current;
            local += phone[s[0]-'0'][i];
            helper(results, s.substr(1, s.length()-1), local, phone);
        }
    }
};

Java版:

public class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> results = new ArrayList<String>();
        if(digits.length() == 0)
            return results;
        List<String> phone = new ArrayList<String>(Arrays.asList("", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"));
        int index = Character.getNumericValue(digits.charAt(0));
        for(int i = 0; i < phone.get(index).length(); i++) {
            String s = "";
            s += phone.get(index).charAt(i);
            helper(results, digits.substring(1), s, phone);    
        }    
        return results;
    }
    
    void helper(List<String> results, String digits, String current, List<String> phone) {
        if(digits.length() == 0) {
            results.add(current);
        } else {
            int index = Character.getNumericValue(digits.charAt(0));
            for(int i = 0; i < phone.get(index).length(); i++) {
                String local = current;
                local += phone.get(index).charAt(i);
                helper(results, digits.substring(1), local, phone);
            }
        }
    }
}

Python版:

class Solution:
    # @return a list of strings, [s1, s2]
    def letterCombinations(self, digits):
        l = []
        if len(digits) == 0:
            return l
        phone = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
        for i in range(0, len(phone[int(digits[0])])):
            c = phone[int(digits[0])][i]
            self.helper(l, digits[1:], c, phone)
        return l
    
    def helper(self, results, digits, current, phone):
        if len(digits) == 0:
            results.append(current)
        else:
            for i in range(0, len(phone[int(digits[0])])):
                local = current
                local += phone[int(digits[0])][i]
                self.helper(results, digits[1:], local, phone)


你可能感兴趣的:(Algorithm,LeetCode,面试题)