93.复原IP地址 (中等)
(从未有过如此美妙的开局,请为我欢呼)
leetcode题目链接:93. 复原 IP 地址 - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址_哔哩哔哩_bilibili
题目描述
解题思路
学习了“131.分割回文串”的思路以后,这道题就迎刃而解了。
首先分割思路上与上述题思路一致。
①回溯函数返回值及参数
string path 用来存储一个合法的ip地址。
vector<string> subpath 用来存储递归路径上的节点。
vector<string> result 用来存放结果。
private:
string path;
vector<string> subpath;
vector<string> result;
void backtracking(const string& s, int startIndex);
②回溯终止条件
if (startIndex == s.size() && subpath.size() == 4)
{
for (int i = 0; i < 4; i++)
{
path += subpath[i];
if (i != 3)
path += '.';
}
result.push_back(path);
path.clear();
}
首先 startIndex == s.size(); 是判断分割线有没有到字符串末尾。subpath.size() == 4; 是用来判断有没有合法的4个子IP串。
③判断是否合法IP子串
bool isIP(const string& s, int front_index, int back_index)
{
string str = s.substr(front_index, back_index - front_index + 1);
int num;
istringstream ss(str);
ss >> num;
if (str.size() == 1)
return true;
if (str.size() != 1)
{
if (*str.begin() == '0')
return false;
if (num >= 0 && num <= 255)
return true;
}
return false;
}
④单层回溯逻辑
for (int i = startIndex; i < s.size(); i++)
{
if (isIP(s, startIndex, i))
subpath.push_back(s.substr(startIndex, i - startIndex + 1));
else
continue;
backtracking(s, i + 1);
subpath.pop_back();
}
题目代码
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
backtracking(s, 0);
return result;
}
private:
void backtracking(const string& s, int startIndex)
{
if (startIndex == s.size() && subpath.size() == 4)
{
for (int i = 0; i < 4; i++)
{
path += subpath[i];
if (i != 3)
path += '.';
}
result.push_back(path);
path.clear();
}
for (int i = startIndex; i < s.size(); i++)
{
if (isIP(s, startIndex, i))
subpath.push_back(s.substr(startIndex, i - startIndex + 1));
else
continue;
backtracking(s, i + 1);
subpath.pop_back();
}
}
bool isIP(const string& s, int front_index, int back_index)
{
string str = s.substr(front_index, back_index - front_index + 1);
int num;
istringstream ss(str);
ss >> num;
if (str.size() == 1)
return true;
if (str.size() != 1)
{
if (*str.begin() == '0')
return false;
if (num >= 0 && num <= 255)
return true;
}
return false;
}
private:
string path;
vector<string> subpath;
vector<string> result;
};
78.子集(中等)
leetcode题目链接:78. 子集 - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集_哔哩哔哩_bilibili
题目描述
解题思路
回溯法的板子,但是本题收集结果是每个节点都在收集结果,而不是叶子节点。
①回溯函数返回值及参数
private:
vector<int> path;
vector<vector<int>> result = { {} };
void backtracking(vector<int>& nums, int startIndex);
②回溯终止条件
if (startIndex == nums.size())
return;
③单层递归逻辑
for (int i = startIndex; i < nums.size(); i++)
{
path.push_back(nums[i]);
result.push_back(path);
backtracking(nums, i + 1);
path.pop_back();
}
题目代码
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
backtracking(nums, 0);
return result;
}
private:
void backtracking(vector<int>& nums, int startIndex)
{
if (startIndex == nums.size())
return;
for (int i = startIndex; i < nums.size(); i++)
{
path.push_back(nums[i]);
result.push_back(path);
backtracking(nums, i + 1);
path.pop_back();
}
}
private:
vector<int> path;
vector<vector<int>> result = { {} };
};
90.子集II
leetcode题目链接:90. 子集 II - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:回溯算法解决子集问题,如何去重?| LeetCode:90.子集II_哔哩哔哩_bilibili
解题思路
子集思路参考上一题,去重思路参考:代码随想录训练营day29|39. 组合总和、40.组合总和II、-CSDN博客中的“组合总和Ⅱ”
题目代码
class Solution {
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(),nums.end());
backtracking(nums, 0);
return result;
}
private:
void backtracking(vector<int>& nums, int startIndex)
{
if (startIndex == nums.size())
return;
for (int i = startIndex; i < nums.size(); i++)
{
if (i > startIndex && nums[i] == nums[i - 1])
continue;
path.push_back(nums[i]);
result.push_back(path);
backtracking(nums, i + 1);
path.pop_back();
}
}
private:
vector<int> path;
vector<vector<int>> result = { {} };
};