LeetCode 0017

原题链接:

https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/

题意理解

给出一些数字组成的字符,要求求出所有的对应的字母的组合。只能说这个题目在刚开始学程序设计的时候就做过了,还是很简单的,就是一个递归。。。

我的代码

class Solution {
    private static String[] table = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

    public List<String> letterCombinations(String digits) {
        List<String> result = new LinkedList<>();
        if(digits.length() == 0) {
            return result;
        }
        int num = digits.charAt(0) - '0';
        dfs(0, "", digits.length() - 1, digits, result);
        return result;
    }

    private void dfs(int step, String s, int max, String digits, List<String> result) {
        if (step > max) {
            result.add(s);
            return;
        }
        int num = digits.charAt(step) - '0';
        for (int i = 0; i < table[num].length(); i++) {
            dfs(step + 1, s + table[num].charAt(i), max, digits, result);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33230935/article/details/79486989
今日推荐