/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char a[8][4]={{"abc "},{"def "},{"ghi "},{"jkl "},{"mno "},{"pqrs"},{"tuv "},{"wxyz"}};
char ** letterCombinations(char * digits, int* returnSize){
*returnSize=0;
char** ans=(char**)calloc(144,sizeof(char*));
if(strlen(digits)==0){
return ans;
}
if(strlen(digits)==1){
for(int b=0;b<4;b++){
if(a[digits[0]-'2'][b]!=' '){
ans[*returnSize]=(char*)calloc(2,sizeof(char));
ans[*returnSize][0]=a[digits[0]-'2'][b];
*returnSize=*returnSize+1;
}
}
}
if(strlen(digits)==2){
for(int b=0;b<4;b++){
if(a[digits[0]-'2'][b]!=' '){
for(int c=0;c<4;c++){
if(a[digits[1]-'2'][c]!=' '){
ans[*returnSize]=(char*)calloc(3,sizeof(char));
ans[*returnSize][0]=a[digits[0]-'2'][b];
ans[*returnSize][1]=a[digits[1]-'2'][c];
*returnSize=*returnSize+1;
}
}
}
}
}
if(strlen(digits)==3){
for(int b=0;b<4;b++){
if(a[digits[0]-'2'][b]!=' '){
for(int c=0;c<4;c++){
if(a[digits[1]-'2'][c]!=' '){
for(int d=0;d<4;d++){
if(a[digits[2]-'2'][d]!=' '){
ans[*returnSize]=(char*)calloc(4,sizeof(char));
ans[*returnSize][0]=a[digits[0]-'2'][b];
ans[*returnSize][1]=a[digits[1]-'2'][c];
ans[*returnSize][2]=a[digits[2]-'2'][d];
*returnSize=*returnSize+1;
}
}
}
}
}
}
}
if(strlen(digits)==4){
for(int b=0;b<4;b++){
if(a[digits[0]-'2'][b]!=' '){
for(int c=0;c<4;c++){
if(a[digits[1]-'2'][c]!=' '){
for(int d=0;d<4;d++){
if(a[digits[2]-'2'][d]!=' '){
for(int e=0;e<4;e++){
if(a[digits[3]-'2'][e]!=' '){
ans[*returnSize]=(char*)calloc(5,sizeof(char));
ans[*returnSize][0]=a[digits[0]-'2'][b];
ans[*returnSize][1]=a[digits[1]-'2'][c];
ans[*returnSize][2]=a[digits[2]-'2'][d];
ans[*returnSize][3]=a[digits[3]-'2'][e];
*returnSize=*returnSize+1;
}
}
}
}
}
}
}
}
}
return ans;
}
力扣-------电话号码的字母组合
最新推荐文章于 2024-10-20 21:47:19 发布