17. Letter Combinations of a Phone Number

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<String> letterCombinations(String digits) {
        int size=digits.length();
        List<String> res=new ArrayList<String>();
        if(size==0)
            return res;
        String[] map = new String[] {  
            " ",  
            "1", "abc", "def",  
            "ghi", "jkl", "mno",  
            "pqrs", "tuv", "wxyz"  
         }; 
        int index=0;
        StringBuilder temp=new StringBuilder();
        bfs(digits,size,index,res,temp,map);
        return res;
        
    }
    
    public void bfs(String digits,int size,int index,List<String> res,StringBuilder temp,String[] map)
    {
        
        if(size==index)
        {
            //表示已经完成了一个字符串的拼接了
            res.add(temp.toString());
            return ;
        }
        
        char c=digits.charAt(index);
        for(int j=0;j<map[c-'0'].length();j++)
        {
            index++;
            temp.append(map[c-'0'].charAt(j));
            bfs(digits,size,index,res,temp,map);
            temp.deleteCharAt(temp.length()-1);
            index--;
        }
    }
}
原文地址:https://www.cnblogs.com/aguai1992/p/5692116.html