LeetCode 第17题:Letter Combinations of a Phone Number

采用递归的思想。回溯法进行的暴力解法:

class Solution {
    public List letterCombinations(String digits) {

        String[] table = new String[]{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        List list = new ArrayList();
        letterCombinations(list,digits,"",0,table);

        return list;
    }

    private void letterCombinations(List list,String digits,String current
                                    ,int index,String[] table){
        if(index == digits.length()){
            if(current.length() != 0){
                list.add(current);
            }
            return;
        }

        String temp = table[digits.charAt(index)-'0'];
        for(int i=0 ; i< temp.length(); i++){
            String next = current + temp.charAt(i);
            letterCombinations(list,digits,next,index+1,table);
        }
    }
}

类似问题,LeetCode 93题、131题

你可能感兴趣的:(算法与数据结构)