[leedcode 17] Letter Combinations of a Phone Number

2638 ワード

public class Solution {
    public List<String> res;
    public StringBuilder seq;
    public List<String> letterCombinations(String digits) {
        ////dfs 。 , level, 
        res=new ArrayList<String>();
        seq=new StringBuilder();
        if(digits.length()==0) return res;
        String[] trans={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        getLetterCom(digits,0,trans);
        return res;
        
    }
    public void getLetterCom(String digits,int level,String trans[]){
        if(level==digits.length()){
            res.add(seq.toString());
            return;
        }
        int n=digits.charAt(level)-'0';
        String temp=trans[n];
        
        for(int i=0;i<temp.length();i++){
            seq.append(temp.charAt(i));
            getLetterCom(digits,level+1,trans);
            seq.deleteCharAt(level);
        }
        
        
    }
}