二刷hot100-17.电话号码的字母组合

思路不难,过程有点繁琐

要预定义一个字符串数组,下标数字与对应元素字符串有着题目所示的映射规律;所以数组的0和1下标对应元素为" ";

这里使用string builder;

递归函数传入一个指针,代表当前过程字符串的长度,

终止条件:当长度与要求长度一致,收割结果

每次进入循环之前都要获取到当前指针所指数字对应的字符串;

以该字符串长度进行循环,每次循环将当前字符串加入中间结果

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"};
        f(digits,numstring,0);
        return res;
    }
    StringBuilder temp = new StringBuilder();
    public void f(String digits,String[] numstring,int index){
        if(index == digits.length()){
            res.add(temp.toString());
            return;
        }
        String s = numstring[digits.charAt(index) - '0'];
        for(int i = 0;i < s.length();i++){
            temp.append(s.charAt(i));
            f(digits,numstring,index + 1);
            temp.deleteCharAt(temp.length() - 1);
        }
    }
}
相关推荐
m0_547486661 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr2 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_32 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.2 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.2 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣2 天前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.9922 天前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
淡海水2 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1012 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_839080542 天前
C++常见八股
数据结构·c++·算法