详细见:leetcode.com/problems/letter-combinations-of-a-phone-number/
Java Solution: github
package leetcode;
import java.util.LinkedList;
import java.util.List;
/*
* 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"].
*/
public class P017_LetterCombinationsOfAPhoneNumb {
public static void main(String[] args) {
System.out.println(new Solution1().letterCombinations("2"));
}
/*
* 2 ms
* 12.81%
*/
static class Solution1 {
char tran[][] = {
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'}
};
StringBuilder st = new StringBuilder();
List<String> ans = new LinkedList<String>();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0)
return ans;
char[] cs = digits.toCharArray();
dfs(cs, 0);
return ans;
}
private void dfs(char[] cs, int i) {
if (i == cs.length) {
ans.add(new String(st));
return;
}
int tran_i = cs[i] - '2';
for (int tran_j = 0; tran_j != tran[tran_i].length; tran_j ++) {
st.append(tran[tran_i][tran_j]);
dfs(cs, i + 1);
st.deleteCharAt(st.length() - 1);
}
}
}
/*
* 1 ms
* 46.16%
*/
static class Solution2 {
char tran[][] = {
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'}
};
char[] st = null;;
List<String> ans = new LinkedList<String>();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0)
return ans;
char[] cs = digits.toCharArray();
st = new char[cs.length];
dfs(cs, 0);
return ans;
}
private void dfs(char[] cs, int i) {
if (i == cs.length) {
ans.add(st.toString());
return;
}
int tran_i = cs[i] - '2';
for (int tran_j = 0; tran_j != tran[tran_i].length; tran_j ++) {
st[i] = tran[tran_i][tran_j];
dfs(cs, i + 1);
}
}
}
}
C Solution: github
/*
url: leetcode.com/problems/letter-combinations-of-a-phone-number/
3ms 0.00%
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void dfs(char ** c, char * d, int di, int dlen, char ** a, int *ai, char * temp, int * cc) {
char ch = *(d + di);
char * t = NULL;
int i = 0;
if (ch == '\0') {
//
t = (char *) malloc(sizeof(char) * (dlen + 1));
for (i = 0; i < dlen + 1; i ++) {
*(t + i) = *(temp + i);
}
*(a + ((*ai)++)) = t;
} else {
for (i = 0; i < *(cc + ch - '2'); i ++) {
*(temp + di) = *(*(c + ch - '2') + i);
dfs(c, d, di + 1, dlen, a, ai, temp, cc);
}
}
}
char** letterCombinations(char* digits, int* returnSize) {
char c0[] = {'a', 'b', 'c'};
char c1[] = {'d', 'e', 'f'};
char c2[] = {'g', 'h', 'i'};
char c3[] = {'j', 'k', 'l'};
char c4[] = {'m', 'n', 'o'};
char c5[] = {'p', 'q', 'r', 's'};
char c6[] = {'t', 'u', 'v'};
char c7[] = {'w', 'x', 'y', 'z'};
int index[] = {0, 0, 0, 1, 1, 1,
2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7};
char * c[8];
int i = 0;
int cc[] = {3, 3, 3, 3, 3, 4, 3, 4};
int len = 0;
int count = 1;
char ** answer = NULL;
char * temp = NULL;
int ai = 0;
c[0] = c0;
c[1] = c1;
c[2] = c2;
c[3] = c3;
c[4] = c4;
c[5] = c5;
c[6] = c6;
c[7] = c7;
len = strlen(digits);
if (len == 0) {
*returnSize = 0;
return NULL;
}
for (i = 0; i < len; i ++) {
count *= cc[*(digits+ i) - '2'];
}
answer = (char **) malloc(sizeof(char *) * count);
temp = (char *) malloc(sizeof(char) * (len + 1));
*(temp + len) = '\0';
dfs(c, digits, 0, len, answer, &ai, temp, cc);
*(returnSize) = count;
return answer;
}
int main() {
char ** a = NULL;
char digits[] = "7";
int returnSize[] = {1};
int i = 0;
a = letterCombinations(digits, returnSize);
for (i = 0; i < *returnSize; i ++)
free(*(a + i));
free(a);
printf("a");
}
#coding=utf-8
'''
url: leetcode.com/problems/letter-combinations-of-a-phone-number/
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年3月28日
@details: Solution: AC 45ms 60.78%
'''
class Solution(object):
def search(self, a, s, t, d, di, dn, p):
if di == dn:
a.append("".join(p))
else:
v = int(d[di]) - 2
for i in range(t[v]):
p[di] = s[v][i]
self.search(a, s, t, d, di + 1, dn, p)
def letterCombinations(self, d):
"""
:type d: str
:rtype: List[str]
"""
a = []
dn = 0 if d == None else len(d)
s = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
t = [3, 3, 3, 3, 3, 4, 3, 4]
p = ['a'] * dn
self.search(a, s, t, d, 0, dn, p)
return a
if __name__ == "__main__":
d = '89'
print(len(Solution().letterCombinations(d)))