力扣 DFS专题

784. 字母大小写全排列

https://leetcode-cn.com/problems/letter-case-permutation/

class Solution {
public:
    vector<string >ans;
    vector<string> letterCasePermutation(string S) {
        dfs("",0,S);
        return ans;
    }
    void dfs(string now,int n,string &s){
        if(n==s.size()) {
            ans.push_back(now);
            return ;
        }
        if(s[n]>='a' && s[n]<='z'){
            //当前字母为小写字母
            now=now+char(s[n]-32);
            dfs(now,n+1,s);
            now.pop_back();
            now=now+s[n];
            dfs(now,n+1,s);
        }
        else if(s[n]>='A' && s[n]<='Z'){
            now=now+char(s[n]+32);
            dfs(now,n+1,s);
            now.pop_back();
            now=now+s[n];
            dfs(now,n+1,s);
        }
        else {
            now=now+s[n];
            dfs(now,n+1,s);
        }
    }
};

77. 组合

https://leetcode-cn.com/problems/combinations/

/*
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
*/
class Solution {
public:
    vector<vector<int>>ans;
    vector<int>now;
    int N,K;
    vector<vector<int>> combine(int n, int k) {
        vector<int> temp;
        N=n,K=k;
        dfs(0,0);
        return ans;
    }
    void dfs(int x,int last){
        if(x==K){
            ans.push_back(now);
            return;
        }
        for(int i=last+1;i<=N;i++){
                now.push_back(i);
                dfs(x+1,i);
                now.pop_back();
        }
    }
};

257. 二叉树的所有路径

https://leetcode-cn.com/problems/binary-tree-paths/

class Solution {
public:
    void construct_paths(TreeNode* root, string path, vector<string>& paths) {
        if (root != nullptr) {
            path += to_string(root->val);
            if (root->left == nullptr && root->right == nullptr) {  // 当前节点是叶子节点
                paths.push_back(path);                              // 把路径加入到答案中
            } else {
                path += "->";  // 当前节点不是叶子节点,继续递归遍历
                construct_paths(root->left, path, paths);
                construct_paths(root->right, path, paths);
            }
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> paths;
        construct_paths(root, "", paths);
        return paths;
    }
};

93. 复原 IP 地址

https://leetcode-cn.com/problems/restore-ip-addresses/

class Solution {
public:
    vector<string> ans;
    vector<int> path;

    vector<string> restoreIpAddresses(string s) {
        dfs(0, 0, s);
        return ans;
    }

    // u表示枚举到的字符串下标,k表示当前截断的IP个数,s表示原字符串
    void dfs(int u, int k, string &s)
    {
        if (u == s.size())
        {
            if (k == 4)
            {
                string ip = to_string(path[0]);
                for (int i = 1; i < 4; i ++ )
                    ip += '.' + to_string(path[i]);
                ans.push_back(ip);
            }
            return;
        }
        if (k > 4) return;

        unsigned t = 0;
        for (int i = u; i < s.size(); i ++ )
        {
            t = t * 10 + s[i] - '0';
            if (t >= 0 && t < 256)
            {
                path.push_back(t);
                dfs(i + 1, k + 1, s);
                path.pop_back();
            }
            if (!t) break;//去掉前导0情况
        }
    }
};

394. 字符串解码

https://leetcode-cn.com/problems/decode-string/submissions/

class Solution {
public:
    bool isNum(char c){
        if(c>='0' && c<='9') return true;
        return false;
    }
    string decodeString(string s) {
        string res;
        for(int i=0;i<s.size();){
            if(!isNum(s[i])) res+=s[i++];
            else {
                int k=0;
                while(isNum(s[i])){
                    k=k*10;
                    k=k+s[i++]-'0';
                }
                int sum=1,j=i+1;
                while(sum>0){
                    //括号匹配计算器
                    if(s[j]=='[') sum++;
                    if(s[j]==']') sum--;
                    j++;
                }
                string r=decodeString(s.substr(i+1,j-i-2));
                while(k--) res+=r;
                i=j;
            }
        }
        return res;
    }
};

posted @ 2021-03-21 16:25  一个经常掉线的人  阅读(45)  评论(0)    收藏  举报