IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++回溯算法之二 -> 正文阅读

[C++知识库]C++回溯算法之二

/*
题目地址:https://leetcode-cn.com/problems/restore-ip-addresses/

给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。

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

例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。

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

示例 2: 输入:s = "0000" 输出:["0.0.0.0"]

示例 3: 输入:s = "1111" 输出:["1.1.1.1"]

示例 4: 输入:s = "010010" 输出:["0.10.0.10","0.100.1.0"]

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

提示: 0 <= s.length <= 3000 s 仅由数字组成
*/


#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std; 

class Solution 
{
public:
    vector<string> result;
    void backtracking( string& str,int index=0,int pointNum=0)
    {         
        if(pointNum == 3)               //控制递归的深度,
        {
            if (valid(str,index,str.size()-1))      //验证最后遗传字符的正确性
            {
                result.push_back(str);
            }
            return;
        }
        for (size_t i = index; i < str.size(); i++)
        {
            if (valid(str, index, i))       //如果字符符合则进入递归,回溯
            {
                str.insert(str.begin() + 1 + i, '.');
                pointNum++;
                //第一轮在i+1位置插入符号'.',所以递归开始会在后面一个位置,所以+2,要吧pointnum传进去,否则默认0,无法控制递归
                backtracking(str, i + 2, pointNum);    
                str.erase(str.begin() + 1 + i);
                pointNum--;
            }
            else break;             //否则直接中断没有意义的循环
        }
      
    }


    bool valid(const string& s,int start,int end)   //判断函数
    {
        if (s[start] == '0') { return false; }; //以0开头的字符为非法字符
        int num = 0;
        for (size_t i = start; i <=end; i++)    //字符串判断的范围取决于start和end
        {
            if (s[i] > '9' || s[i] < '0')   //判断非法字符
            {
                return false;
            }          
            num = num * 10 + (s[i] - '0');  //判断是否超过ip最大值255
            if (num>255)
            {
                return false;
            }
        } 
        return true;
    }

    vector<string> Call(string s)
    {
        backtracking(s,0,0);
        return result;
    }
};

int main()
{
    Solution Test;
    auto num = Test.Call("1921081312");

    for (auto s : num) {
       // for (auto c : s)  
         cout << s <<ends; 
    };

}

---------------------------------------
/*
题目地址:https://leetcode-cn.com/problems/subsets/

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [1,2,3],   [1,3],   [2,3],   [1,2],   [] ]
*/
#include <iostream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

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) {
        
        backtracking(nums, 0);
        return result;
    }
};


int main()
{
    vector<int> nums = {1,2,3};
    Solution Test;
    auto num = Test.subsets(nums);

    for (auto s : num) {
        for (auto c : s)
        {
            cout << c << ends;
        }
        cout << endl;
    };

}

/*
题目链接:https : //leetcode-cn.com/problems/permutations/

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1] ,2], [3,2,1] ]

*/

class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking (vector<int>& nums, vector<bool>& used) {
        // 此时说明找到了一组
        if (path.size() == nums.size()) {
            result.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (used[i] == true) continue; // path里已经收录的元素,直接跳过
            used[i] = true;
            path.push_back(nums[i]);
            backtracking(nums, used);
            path.pop_back();
            used[i] = false;
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        result.clear();
        path.clear();
        vector<bool> used(nums.size(), false);
        backtracking(nums, used);
        return result;
    }
};




  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:32:07  更:2021-07-23 10:33:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 17:13:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码