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번

좋은 웹페이지 즐겨찾기