leetcode17+stringbuilder와 귀속

1497 단어 귀속
import java.util.ArrayList;
import java.util.List;

public class LetterCombinations_17 {
    private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    List solution(String digits){

        List combinations = new ArrayList<>();

        if ( digits==null||digits.length()==0){
            return combinations;
        }
        doCombination(new StringBuilder(),combinations,digits);


        return combinations;
    }

    private void doCombination(StringBuilder stringBuilder, List combinations, String digits) {
        if (stringBuilder.length()==digits.length()){
            combinations.add(stringBuilder.toString());
            return;
        }
        // keys index 
        int curDigits = digits.charAt(stringBuilder.length())-'0';// stringbuilder , 
        String letters = KEYS[curDigits];
        for (char m : letters.toCharArray()){
            stringBuilder.append(m);// 
            doCombination(stringBuilder,combinations,digits);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
            // ,23 ad,ae,af, def ,
            //  234 3 , ad adg,adh,adi ghi 
        }
    }

    public static void main(String[] args) {
        new LetterCombinations_17().solution("23");
        System.out.println("dd");
    }

}

좋은 웹페이지 즐겨찾기