3211. 生成不含相邻零的二进制字符串
题目链接:3211. 生成不含相邻零的二进制字符串
代码如下:
class Solution
{
public:
vector<string> validStrings(int n)
{
vector<string> res;
string path(n, 0);
auto dfs = [&](auto&& dfs, int i)->void
{
if (i == n)
{
res.push_back(path);
return;
}
//填1
path[i] = '1';
dfs(dfs, i + 1);
//填0
if (i == 0 || path[i - 1] == '1')
{
path[i] = '0';//直接覆盖
dfs(dfs, i + 1);
}
};
dfs(dfs, 0);
return res;
}
};