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

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

相关推荐
在风中的意志1 小时前
[数据库SQL] [leetcode-584] 584. 寻找用户推荐人
数据库·sql·leetcode
毅炼1 小时前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先
leoufung7 小时前
LeetCode 67. Add Binary:从面试思路到代码细节
算法·leetcode·面试
无限进步_7 小时前
【C语言】循环队列的两种实现:数组与链表的对比分析
c语言·开发语言·数据结构·c++·leetcode·链表·visual studio
linsa_pursuer7 小时前
最长连续序列
java·数据结构·算法·leetcode
POLITE38 小时前
Leetcode 54.螺旋矩阵 JavaScript (Day 8)
javascript·leetcode·矩阵
only-qi9 小时前
LeetCode 148. 排序链表
算法·leetcode·链表
smj2302_796826529 小时前
解决leetcode第3791题.给定范围内平衡整数的数目
python·算法·leetcode
不能只会打代码9 小时前
力扣--1970. 你能穿过矩阵的最后一天(Java)
java·算法·leetcode·二分查找·力扣·bfs·最后可行时间
光明西道45号9 小时前
Leetcode 15. 三数之和
数据结构·算法·leetcode