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

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

相关推荐
有泽改之_5 小时前
leetcode146、OrderedDict与lru_cache
python·leetcode·链表
im_AMBER5 小时前
Leetcode 74 K 和数对的最大数目
数据结构·笔记·学习·算法·leetcode
长安er6 小时前
LeetCode 206/92/25 链表翻转问题-“盒子-标签-纸条模型”
java·数据结构·算法·leetcode·链表·链表翻转
Benmao⁢6 小时前
C语言期末复习笔记
c语言·开发语言·笔记·leetcode·面试·蓝桥杯
CoderYanger6 小时前
动态规划算法-01背包问题:50.分割等和子集
java·算法·leetcode·动态规划·1024程序员节
菜鸟233号8 小时前
力扣513 找树左下角的值 java实现
java·数据结构·算法·leetcode
leoufung8 小时前
LeetCode 22:Generate Parentheses 题解(DFS / 回溯)
算法·leetcode·深度优先
FMRbpm8 小时前
队列练习--------最近的请求次数(LeetCode 933)
数据结构·c++·leetcode·新手入门
长安er10 小时前
LeetCode 34排序数组中查找元素的第一个和最后一个位置-二分查找
数据结构·算法·leetcode·二分查找·力扣
CoderYanger10 小时前
动态规划算法-两个数组的dp(含字符串数组):48.最长重复子数组
java·算法·leetcode·动态规划·1024程序员节