首页 > 编程语言 >代码随想录算法 - 回溯3

代码随想录算法 - 回溯3

时间:2024-09-20 20:46:56浏览次数:1  
标签:nums IP 示例 随想录 算法 vector 子集 result 回溯

明天开始建模比赛,没时间写思路了

题目1 93. 复原 IP 地址

有效 IP 地址 正好由四个整数(每个整数位于 0255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

  • 例如:"0.1.2.201" "192.168.1.1"有效 IP 地址,但是 "0.011.255.245""192.168.1.312""[email protected]"无效 IP 地址。

给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 '.' 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

示例 1:

输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]

示例 2:

输入:s = "0000"
输出:["0.0.0.0"]

示例 3:

输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

提示:

  • 1 <= s.length <= 20
  • s 仅由数字组成

代码

class Solution {
public:
    string str;
    vector<string> path;
    vector<string> result;
    bool ispartAddr()
    {
        if(str[0] == '0' && str.size() > 1)
            return false;
        int num = atoi(str.c_str());
        if(num >= 0 && num <=255)
            return true;
        else
            return false;
    }
    void backtracking(string& s, int curIndex)
    {
        if(path.size() == 4 && curIndex == s.size())
        {
            str = "";
            for(int i = 0; i < path.size() - 1; i++)
                str += path[i] + ".";
            str += path[3];
            result.push_back(str);
            return;
        }
        for(int i = curIndex; i < s.size(); i++)
        {
            str = s.substr(curIndex, i - curIndex + 1);
            if(!ispartAddr())
                break;
            path.push_back(str);
            backtracking(s, i + 1);
            path.pop_back();
        }
    }
    vector<string> restoreIpAddresses(string s) {
        backtracking(s, 0);
        return result;
    }
};

题目2 78. 子集

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的

子集

(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同

代码

class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;
    void backtracking(vector<int>& nums, int curIndex)
    {
        if(curIndex <= nums.size())
        {
            result.push_back(path);
        }
        for(int i = curIndex; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        backtracking(nums, 0);
        return result;
    }
};

题目3 90. 子集 II

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的

子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

代码

class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;
    void backtracking(vector<int>& nums, int startIndex)
    {
        if(startIndex <= nums.size())
        {
            result.push_back(path);
        }
        for(int i = startIndex; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            backtracking(nums, i + 1);
            path.pop_back();
            while(i + 1 < nums.size() && nums[i + 1] == nums[i])
                i++;
        }
    }
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        backtracking(nums, 0);
        return result;
    }
};

标签:nums,IP,示例,随想录,算法,vector,子集,result,回溯
From: https://www.cnblogs.com/code4log/p/18423245

相关文章