二进制手表(简单)
力扣题401
这一题还是用DFS(深度优先搜索), 但是是组合问题。
使用DFS如何解题呢?
class Solution {
public:
//cur表示用的是当前bit位, 组合的核心就是:
//当前的bit位用了, 后面的盒子只能填后面的bit位
void DFS(int n, vector<string>& ans, vector<int>& book, int cur)
{
//表示盒子填满了
if(n == 0)
{
int hour = book[0] * 1 + book[1] * 2 + book[2] * 4 + book[3] * 8;
int minute = book[4] * 1 + book[5] * 2 + book[6] * 4 +
book[7] * 8 + book[8] * 16 + book[9] * 32;
if(hour < 12 && minute < 60)
{
string s;
s += to_string(hour);
s += ":";
if(minute < 10)
s += "0";
s += to_string(minute);
ans.push_back(s);
}
return;
}
for(int i = cur; i < 10; i++)
{
if(book[i] == 0)
{
book[i] = 1;
DFS(n - 1, ans, book, i + 1);
book[i] = 0;
}
}
}
vector<string> readBinaryWatch(int turnedOn)
{
vector<int> book(10, 0);
vector<string> ans;
DFS(turnedOn, ans, book,0);
return ans;
}
};
使用常规的解法,就是简单的遍历:
class Solution {
public:
int getBitOne(int n)
{
int count = 0;
while(n)
{
n = n & (n-1);
count++;
}
return count;
}
vector<string> readBinaryWatch(int turnedOn)
{
vector<string> ans;
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 60; j++)
{
if(getBitOne(i) + getBitOne(j) == turnedOn)
{
string s = to_string(i);
s += ":";
if(j < 10)
s += "0";
s += to_string(j);
ans.push_back(s);
}
}
}
return ans;
}
};
力扣39. 组合总和(中等)
而且的话,题目的默认条件是升序。
class Solution {
public:
vector<vector<int>> ans;
void DFS( vector<int> v, const vector<int>& candidates, int target, int cur, int curSum)
{
if(curSum >= target)
{
if(curSum == target)
ans.push_back(v);
return;
}
for(int i = cur; i < candidates.size(); i++)
{
if(candidates[i] > target)
continue;
//去重
if(i - 1 >= 0 && candidates[i - 1] == candidates[i])
continue;
v.push_back(candidates[i]);
//这里的下标i是使用的位置, 然后还是可以使用的
DFS(v, candidates, target, i, curSum + candidates[i]);
v.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
vector<int> v;
DFS(v, candidates, target, 0, 0);
return ans;
}
};
力扣1079,活字印刷
class Solution {
public:
int cnt = 0;
void DFS(const string& tiles, vector<bool>& book)
{
for(int i = 0; i < tiles.size(); i++)
{
//这个是同一层的去重
if(i - 1 >= 0 && tiles[i - 1] == tiles[i]
&& book[ i - 1] == false)
continue;
if(book[i] == false)
{
book[i] = true;
cnt++;
DFS(tiles, book);
book[i] = false;
}
}
}
int numTilePossibilities(string tiles)
{
// 示例给的是排好序的, 实际上并不会
sort(tiles.begin(), tiles.end());
vector<bool> book(tiles.size(), 0);
DFS(tiles, book);
return cnt;
}
};
力扣51,n皇后问题
class Solution {
public:
void DFS(int n, int curRow, vector<pair<int, int>> ans,
vector<vector<pair<int, int>>>& allSolution)
{
if(curRow == n )
{
allSolution.push_back(ans);
return;
}
for(int i = 0; i < n; i++)
{
if(isValid(ans, curRow, i))
{
ans.push_back({curRow, i});
DFS(n, curRow + 1, ans, allSolution);
ans.pop_back();
}
}
}
bool isValid(const vector<pair<int, int>>& ans, int row, int col)
{
for(auto e : ans)
{
if(e.second == col || e.first - e.second == row -col
|| e.first + e.second == row + col)
return false;
}
return true;
}
void tansferResult(vector<vector<string>>& result, vector<vector<pair<int, int>>>& allSolution, int n)
{
for(auto& e1 : allSolution)
{
vector<string> ret(n, string(n, '.'));
for(auto& e2 : e1)
{
ret[e2.first][e2.second] = 'Q';
}
result.push_back(ret);
}
}
vector<vector<string>> solveNQueens(int n)
{
vector<pair<int, int>> ans;
vector<vector<pair<int, int>>> allSolution;
DFS(n, 0, ans, allSolution);
vector<vector<string>> result;
tansferResult(result, allSolution, n);
return result;
}
};