1.784. 字母大小写全排列 - 力扣(LeetCode)
class Solution {
public:
vector<string>ret;
string path;
char sw(char a)
{
if(a>='A'&&a<='Z')
{
return a+32;
}
return a-32;
}
void dfs(string s,int pos)
{
if(pos==s.size())
{
ret.push_back(path);
return;
}
path.push_back(s[pos]);
dfs(s,pos+1);
path.pop_back();
if(s[pos]>='A'&&s[pos]<='Z'||s[pos]>='a'&&s[pos]<='z')
{
char a=sw(s[pos]);
path.push_back(a);
dfs(s,pos+1);
path.pop_back();
}
}
vector<string> letterCasePermutation(string s) {
dfs(s,0);
return ret;
}
};
2.526. 优美的排列 - 力扣(LeetCode) (注意括号)
class Solution {
public:
int ret=0;
bool check[20]={false};
void dfs(int n,int pos)
{
if(pos==n+1)
{
ret++;
return;
}
for(int i=1;i<=n;i++)
{
if(check[i]==false&&(pos%i==0||i%pos==0))
{
check[i]=true;
dfs(n,pos+1);
check[i]=false;
}
}
}
int countArrangement(int n) {
dfs(n,1);
return ret;
}
};
class Solution {
public:
vector<vector<string>>ret;
vector<string>path;
bool judge(vector<string>path,int row,int col,int n )
{
for(int i=0;i<row;i++)
{
if(path[i][col]=='Q')
{
return false;
}
}
for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--)
{
if(path[i][j]=='Q')
{
return false;
}
}
for(int i=row-1,j=col+1;i>=0&&j<n;i--,j++)
{
if(path[i][j]=='Q')
{
return false;
}
}
return true;
}
void dfs(int line,int n)
{
if(line==n)
{
ret.push_back(path);
return ;
}
for(int i=0;i<n;i++)
{
if(judge(path,line,i,n)==true)
{
path[line][i]='Q';
dfs(line+1,n);
path[line][i]='.';
}
}
}
vector<vector<string>> solveNQueens(int n) {
string good(n,'.');
path.resize(n,good);
dfs(0,n);
return ret;
}
};