LeetCode热题100(电话号码的字母组合)

题目描述

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

题解思路:

java 复制代码
class Solution {
    List<String> res = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if(digits == null || digits.length() == 0){
            return res;
        }
        String[] numString = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        helper(digits,numString,0);
        return res;
    }
    StringBuilder temp = new StringBuilder();
    public void helper(String digits,String[] numString,int num){
        if(num == digits.length()){
            res.add(temp.toString());
            return;
        }
        String str = numString[digits.charAt(num)-'0'];
        for(int i = 0; i <str.length();i++){
            temp.append(str.charAt(i));
            helper(digits,numString,num+1);
            temp.deleteCharAt(temp.length() - 1);
        }
    }
}

可以对比之前子集的处理思路是类似,这里比较巧妙的是吧数字和字符串做了影射。

相关推荐
Pniubi2 小时前
力扣19 . 删除表的倒数第N个节点(栈)
算法·leetcode·职场和发展
文丑颜不良啊17 小时前
LeetCode2761~2780题目解析
java·leetcode
文丑颜不良啊20 小时前
LeetCode2841~2860题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2921~2940题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2901~2920题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2861~2880题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2821~2840题目解析
java·leetcode
Pniubi2 天前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
文丑颜不良啊2 天前
LeetCode2741~2760题目解析
java·leetcode
俊俊几年的2 天前
动态规划(Dynamic Programming)详解:从入门到 LeetCode Hot 100
linux·c++·算法·leetcode·嵌入式·动态规划·c