代码随想录—Day24

93.复原IP地址(这题有点难 0.0)

class Solution {
private:
    vector<string> result;
    void backtracking(string& s,int startIndex,int poinNum){
        if(poinNum == 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)){
                s.insert(s.begin()+i+1,'.');
                poinNum++;
                backtracking(s,i+2,poinNum);
                poinNum--;
                s.erase(s.begin()+i+1);
            }else{
                break;
            }
        }
    }

    bool isValid(const string& s,int start,int end){
        if(start > end){
            return false;
        }
        if(s[start] == '0' && start != end){
            return false;
        }
        int num = 0;
        for(int i =start;i<=end;i++){
            if(s[i] > '9' || s[i] < '0'){
                return false;
            }
            num = num*10 +(s[i] - '0');
            if(num >255){
                return false;
            }
        }
        return true;
    }


public:
    vector<string> restoreIpAddresses(string s) {
        result.clear();
        if(s.size()<4 || s.size()>12){
            return result;
        }
        backtracking(s,0,0);
        return result;
    }
};

78.子集

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.子集||

class Solution {
private:
    vector<vector<int>> result;
    vector<int> path;

    void backtracking(vector<int>& nums,int startIndex,vector<bool>& used){
        result.push_back(path);
        if(startIndex == nums.size()){
            return;
        }
        for(int i = startIndex;i<nums.size();i++){
            if(i>0 && nums[i]==nums[i-1] && used[i-1] == false){
                continue;
            }
            path.push_back(nums[i]);
            used[i] = true;
            backtracking(nums,i+1,used);
            used[i] = false;
            path.pop_back();
        }
    }
    

public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        result.clear();
        path.clear();
        vector<bool> used(nums.size(),false);
        sort(nums.begin(),nums.end());
        backtracking(nums,0,used);
        return result;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值