美文网首页
17. Letter Combinations of a Pho

17. Letter Combinations of a Pho

作者: wtmxx | 来源:发表于2018-02-08 00:04 被阅读0次

思路:使用递归

class Solution {
    public static Map<Character,char[]> map = new HashMap<>();
    {   
        char[] tmp1 = {};
        map.put('1',tmp1);
        char[] tmp2={'a','b','c'};
        map.put('2',tmp2);
        char[] tmp3={'d','e','f'};
        map.put('3',tmp3);
        char[] tmp4={'g','h','i'};
        map.put('4',tmp4);
        char[] tmp5={'j','k','l'};
        map.put('5',tmp5);
        char[] tmp6={'m','n','o'};
        map.put('6',tmp6);
        char[] tmp7={'p','q','r','s'};
        map.put('7',tmp7);
        char[] tmp8={'t','u','v'};
        map.put('8',tmp8);
        char[] tmp9={'w','x','y','z'};
        map.put('9',tmp9);
    }
    public List<String> letterCombinations(String digits) {
        List<String> zz = new ArrayList<>();
        if(digits.length()==0)
            return zz;
        addToList(0,digits,zz,"");
        return zz;
    }
    public void addToList(int i,String digits,List<String> zz,String fuck){
        if(i==digits.length()){
            zz.add(fuck);
            return;
        }
        for(int j=0;j<map.get(digits.charAt(i)).length;j++){
            addToList(i+1,digits,zz,fuck+map.get(digits.charAt(i))[j]);
        }
    }
}

相关文章

网友评论

      本文标题:17. Letter Combinations of a Pho

      本文链接:https://www.haomeiwen.com/subject/pdjzzxtx.html