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);
        }
    }
}

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

相关推荐
失去的青春---夕阳下的奔跑12 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
m0_6294947313 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表
吃着火锅x唱着歌15 小时前
LeetCode 1019.链表中的下一个更大节点
算法·leetcode·链表
凌波粒15 小时前
LeetCode--404.左叶子之和(二叉树)
算法·leetcode·职场和发展
绝知此事16 小时前
【算法突围 03】核心算法思想:分治/递归/动态规划与 LeetCode 高频真题解析
算法·leetcode·面试·动态规划
阿Y加油吧17 小时前
两道字符串 DP 模板题复盘:最长公共子序列 & 编辑距离
leetcode
我爱cope17 小时前
【力扣hot100:76. 最小覆盖子串】
算法·leetcode·职场和发展
sheeta199818 小时前
LeetCode 每日一题笔记 日期:2026.05.20 题目:2657. 找到前缀公共数组
笔记·算法·leetcode
吃着火锅x唱着歌19 小时前
LeetCode 962.最大宽度坡
算法·leetcode·职场和发展
凌波粒19 小时前
LeetCode--257. 二叉树的所有路径(二叉树)
算法·leetcode·职场和发展