1.找出所有子集的异或总和再求和
版本一:
class Solution {
public:
int sum;
int path;
int subsetXORSum(vector<int>& nums) {
dfs(nums, 0);
return sum;
}
void dfs(vector<int>& nums, int pos)
{
if(pos == nums.size())
{
sum += path;
return;
}
//选
path ^= nums[pos];
dfs(nums, pos + 1);
path ^= nums[pos];
//不选
dfs(nums, pos + 1);
}
};
版本二:
class Solution {
public:
int sum;
int path;
int subsetXORSum(vector<int>& nums) {
dfs(nums, 0);
return sum;
}
void dfs(vector<int>& nums, int pos)
{
sum += path;
for(int i = pos; i < nums.size(); i++)
{
path ^= nums[i];
dfs(nums, i + 1);
path ^= nums[i];
}
}
};
2.全排列ii
剪枝:
class Solution {
public:
vector<vector<int>> ret;
vector<int> path;
bool check[8];
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
dfs(nums, 0);
return ret;
}
void dfs(vector<int>& nums, int pos)
{
if(path.size() == nums.size())
{
ret.push_back(path);
return;
}
for(int i = 0; i < nums.size(); i++)
{
//两种剪枝方法
//只关注不合法情况
//if(check[i] != false || (i > 0 && nums[i] == nums[i - 1] && check[i - 1] == false))
// continue;
// path.push_back(nums[i]);
// check[i] = true;
// dfs(nums, i + 1);
// path.pop_back();
// check[i] = false;
//只关注合法情况
if((check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1]) != false))
{
path.push_back(nums[i]);
check[i] = true;
dfs(nums, i + 1);
path.pop_back();
check[i] = false;
}
}
}
};
3.电话号码的字母组合
class Solution {
public:
string hash[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> ret;
string path;
vector<string> letterCombinations(string digits) {
if(digits.size() == 0) return ret;
dfs(digits, 0);
return ret;
}
void dfs(string digits, int pos)
{
if(pos == digits.size())
{
ret.push_back(path);
return;
}
for(char ch : hash[digits[pos] - '0'])
{
path.push_back(ch);
dfs(digits, pos + 1);
path.pop_back();//恢复现场
}
}
};
4.括号生成
有效括号?
class Solution {
public:
int _n;
vector<string> ret;
string path;
int left, right;
vector<string> generateParenthesis(int n) {
_n = n;
dfs();
return ret;
}
void dfs()
{
if(right == _n)
{
ret.push_back(path);
return;
}
//增加左括号
if(left < _n)
{
path.push_back('(');left++;
dfs();
path.pop_back();left--;
}
//增加右括号
if(left > right)
{
path.push_back(')'); right++;
dfs();
path.pop_back(); right--;
}
}
};
5.组合
class Solution {
public:
int _n, _k;
vector<vector<int>> ret;
vector<int> path;
vector<vector<int>> combine(int n, int k) {
_n = n;
_k = k;
dfs(1);
return ret;
}
void dfs(int pos)
{
if(path.size() == _k)
{
ret.push_back(path);
return;
}
for(int i = pos; i <= _n; i++)
{
path.push_back(i);
dfs(i + 1);
path.pop_back();
}
}
};
6.目标和
class Solution {
public:
int aim;
int ret;
int findTargetSumWays(vector<int>& nums, int target) {
aim = target;
dfs(nums, 0, 0);
return ret;
}
void dfs(vector<int>& nums, int pos, int path)
{
if(pos == nums.size())
{
if(path == aim) ret++;
return;
}
//path做全局变量版本
// //添加+
// path += nums[pos];
// dfs(nums, pos + 1);
// path -= nums[pos];
// //添加-
// path -= nums[pos];
// dfs(nums, pos + 1);
// path += nums[pos];
//path做参数版本
dfs(nums, pos + 1, path + nums[pos]);
dfs(nums, pos + 1, path - nums[pos]);
}
};