My java solution 4ms backtracking


  • 0
    T
    String phone[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits.length()==0)
            return res;
        String str = "";
        generate(digits,str,res);
        return res;
    }
    public void generate(String digits,String temp,List<String> list){
        if (digits.length()<1){
            list.add(temp);
            return;
        }
        int curr=Character.getNumericValue(digits.charAt(0));
        int len = phone[curr].length();
        for (int i = 0; i < len; i++) {
                generate(digits.substring(1),temp+phone[curr].charAt(i),list);
        }
    }

  • 0

    can you explain
    int curr=Character.getNumericValue(digits.charAt(0));
    int len = phone[curr].length();
    for (int i = 0; i < len; i++) {
    generate(digits.substring(1),temp+phone[curr].charAt(i),list);
    }
    Thank you!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.