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

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

相关推荐
海清河晏11116 小时前
加油站问题
数据结构·算法·leetcode
Nuanyt18 小时前
LeetCode Hot 100 · 双指针 3 题 Java
java·笔记·算法·leetcode·双指针
文丑颜不良啊19 小时前
LeetCode851~875题目解析
java·leetcode
Navigator_Z19 小时前
LeetCode //C - 1304. Find N Unique Integers Sum up to Zero
c语言·算法·leetcode
?abc!1 天前
(数组)581. 最短无序连续子数组
数据结构·算法·leetcode
hold?fish:palm1 天前
40 二叉树的直径
c++·leetcode
guo_wen_qiang1 天前
二叉树的层序遍历-使用双端队列Deque
算法·leetcode·1024程序员节
爱读书的小胖1 天前
LeetCode 55 跳跃游戏 C 语言解题分析
c语言·数据结构·算法·leetcode·1024程序员节
Navigator_Z2 天前
LeetCode //C - 1300. Sum of Mutated Array Closest to Target
c语言·算法·leetcode
凯瑟琳.奥古斯特2 天前
力扣1016高效解法:剪枝优化技巧
开发语言·c++·算法·leetcode·职场和发展