代码随想录算法训练营第二十五天 | 216.组合总和III,17.电话号码的字母组合

分段排列,有点像乘法原理,各个区间的顺序确定,但是区间的内部元素不确定,针对各个区间回溯,区间之间相互独立

cpp 复制代码
class Solution {
public:
    vector<string> res;
    string resStr;
    vector<string> chooseList;
    void resInit(){
        for(int i = 0;i< 10;i++){
            string chooseStr ="";
            switch(i){
                case 2:{
                    chooseStr = "abc";
                    break;
                }
                case 3:{
                    chooseStr = "def";
                     break;
                }
                case 4:{
                    chooseStr = "ghi";
                     break;
                }
                case 5:{
                    chooseStr = "jkl";
                    break;
                }
                case 6:{
                    chooseStr = "mno";
                     break;
                }
                case 7:{
                    chooseStr = "pqrs";
                     break;
                }
                 case 8:{
                    chooseStr = "tuv";
                     break;
                }
                case 9:{
                    chooseStr = "wxyz";
                     break;
                }
                default:
                     break;
            }

            chooseList.push_back(chooseStr);
        }
    }
    bool isValid();
    void traceback(string resStr,string & digits,int seq){

        if(resStr.size() == digits.size()){
           res.push_back(resStr);
           return;
        }

        for(auto j : chooseList[(digits[seq] -'0')]){
              resStr.push_back(j);
              traceback(resStr,digits,seq+1);
              resStr.pop_back();
        }
    }
    vector<string> letterCombinations(string digits) {
        resInit();
        if(digits == ""){
            return res;
        }
        traceback(resStr,digits,0);
        return res;
    }
};


无重组合,不可复取

cpp 复制代码
class Solution {  
public:
    vector<vector<int>> res;
    vector<int> resVec;
    vector<int> chooseList;
    void resInit(){
        for(int i =0;i< 9;i++){
            chooseList.push_back(i+1);
        }
    }
    bool isValid();
    void traceback(vector<int> resVec,vector<int> &chooseList,int &n,int &k,int sum,int start){
        if(k == resVec.size()){
            if(sum == n){
                res.push_back(resVec);
            }
            return;
        }
        
        for(int i =start;i< chooseList.size();i++){
            resVec.push_back(chooseList[i]);
            traceback(resVec,chooseList,n,k,sum+chooseList[i],i+1);
            resVec.pop_back();
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
           resInit();
            traceback(resVec,chooseList,n,k,0,0);
           return res;
    }
};
相关推荐
旖旎夜光21 小时前
C++(17)
c++·学习
Larry_Yanan1 天前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
superman超哥1 天前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
LYFlied1 天前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠1 天前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Lucas555555551 天前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
ytttr8731 天前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
_MyFavorite_1 天前
cl报错+安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
charlie1145141911 天前
现代嵌入式C++教程:C++98——从C向C++的演化(2)
c语言·开发语言·c++·学习·嵌入式·教程·现代c++
zmzb01031 天前
C++课后习题训练记录Day55
开发语言·c++