代码随想录算法训练营Day27 || leetCode 93.复原IP地址 || 78.子集 || 90.子集II

93.复原IP地址

与分割回文串的代码相近,先写出判断函数,之后以判断结果为标准,执行回溯的代码。此题中使用点的个数来决定回溯的终止,需要学习一下。

cpp 复制代码
class Solution {
private:
    vector<string> result;
    bool isValid(const string& s,int startIndex,int endIndex){
        if (startIndex > endIndex) return false;
        if (s[startIndex] == '0' && startIndex != endIndex) return false;
        int num=0;
        for (int i = startIndex; i <= endIndex; i++){
            if (s[i] > '9' || s[i] < '0') return false;
            num = 10*num + (s[i] - '0');
            if (num > 255) return false;
        }
        return true;
    }
    void backtracking(string& s, int startIndex, int pointNum){
        if (pointNum == 3) {
            if (isValid(s,startIndex,s.size()-1)){
                result.push_back(s);
            }
            return;            
        }
        for (int i = startIndex; i < s.size(); i++){
            if (isValid(s, startIndex, i)) { // 判断 [startIndex,i] 这个区间的子串是否合法
                s.insert(s.begin() + i + 1 , '.');  // 在i的后面插入一个逗点
                pointNum++;
                backtracking(s, i + 2, pointNum);   // 插入逗点之后下一个子串的起始位置为i+2
                pointNum--;                         // 回溯
                s.erase(s.begin() + i + 1);         // 回溯删掉逗点
            } else break; // 不合法,直接结束本层循环
        }
    }
public:
    vector<string> restoreIpAddresses(string s) {
        result.clear();
        if (s.size() < 4 || s.size() > 12) return result; // 算是剪枝了
        backtracking(s, 0, 0);
        return result;
    }
};

78.子集

子集是组合,所以要求我们无重复的取值,同时与此前不同的是,此前的组合是规定了答案长度的,此处没有,所以需要我们把所有的节点都进行保存。

cpp 复制代码
class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& nums, int startIndex) {
        result.push_back(path); // 收集子集,要放在终止添加的上面,否则会漏掉自己
        if (startIndex >= nums.size()) { // 终止条件可以不加
            return;
        }
        for (int i = startIndex; i < nums.size(); i++) {
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        result.clear();
        path.clear();
        backtracking(nums, 0);
        return result;
    }
};

90.子集II

使用40题组合总和II中的去重思路来写

cpp 复制代码
class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking(vector<int>& nums, int startIndex) {
        result.push_back(path);
        for (int i = startIndex; i < nums.size(); i++) {
            // 而我们要对同一树层使用过的元素进行跳过
            if (i > startIndex && nums[i] == nums[i - 1] ) { // 注意这里使用i > startIndex
                continue;
            }
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }

public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        result.clear();
        path.clear();
        sort(nums.begin(), nums.end()); // 去重需要排序
        backtracking(nums, 0);
        return result;
    }
};
相关推荐
小糯米6017 分钟前
C++ 单调栈原理与模板
开发语言·c++·算法
常利兵8 分钟前
Android 集合探秘:ArrayMap 与 SparseArray 的奇妙之旅
android·算法·哈希算法
滴滴答滴答答10 分钟前
LeetCode Hot100 之 41 缺失的第一个正数
算法·leetcode·职场和发展
rgb2gray10 分钟前
论文详解:基于POI与出租车轨迹的城市多中心结构静态-动态多重分形特征
人工智能·python·算法·机器学习·数据分析·可解释
Zarek枫煜10 分钟前
[特殊字符]栈(Stack)原理详解 \+ Zig / C3 双语言实现
c语言·单片机·嵌入式硬件·算法
jz_ddk15 分钟前
[实战] CIC滤波器设计与实现
人工智能·算法·机器学习·数字信号处理·cic滤波器
Sakinol#18 分钟前
Leetcode Hot 100 ——多维动态规划
算法·leetcode·动态规划
xsyaaaan18 分钟前
leetcode-hot100-二叉树
数据结构·leetcode
XZXZZX21 分钟前
ATCODER ABC 450 C题解
c++·算法·ccf csp
堕27424 分钟前
JavaEE初阶——《多线程--. 多线程带来的的⻛险-线程安全 (重点)》
java·算法·java-ee