36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
//模拟,就是简单判断下有没有重复
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
map<char, int> mp;
for(int i = 0; i < 9; i++)
{
mp.clear();
for(int j = 0; j < 9; j++)
{
if(board[i][j] == '.') continue;
mp[board[i][j]]++;
if(mp[board[i][j]] == 2) return false;
}
mp.clear();
for(int j = 0; j < 9; j++)
{
if(board[j][i] == '.') continue;
mp[board[j][i]]++;
if(mp[board[j][i]] == 2) return false;
}
}
for(int i = 0; i < 9; i += 3)
{
for(int j = 0; j < 9; j += 3)
{
mp.clear();
for(int k = i; k < i+3; k++)
{
for(int kk = j; kk < j+3; kk++)
{
if(board[k][kk] == '.') continue;
mp[board[k][kk]]++;
if(mp[board[k][kk]] == 2) return false;
}
}
}
}
return true;
}
};
37
.
Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'
.
You may assume that there will be only one unique solution.
A sudoku puzzle...
...and its solution numbers marked in red.
//暴力
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board, int i, int j) {
for(int k = 0; k < 9; k++)
{
if(k == i) continue;
if(board[k][j] == board[i][j]) return false;
}
for(int k = 0; k < 9; k++)
{
if(k == j) continue;
if(board[i][k] == board[i][j]) return false;
}
for(int k = i/3*3; k < i/3*3+3; k++)
{
for(int kk = j/3*3; kk < j/3*3+3; kk++)
{
if(k != i && kk != j && board[k][kk] == board[i][j]) return false;
}
}
return true;
}
bool solveSudoku(vector<vector<char>>& board) {
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(board[i][j] == '.')
{
for(int k = 1; k <= 9; k++)
{
board[i][j] = k+'0';
if(isValidSudoku(board, i, j) && solveSudoku(board)) return true;
board[i][j] = '.';
}
return false;
}
}
}
return true;
}
};
38
.
Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1 2. 11 3. 21 4. 1211 5. 111221
1
is read off as "one 1"
or 11
.
11
is read off as "two 1s"
or 21
.
21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1 Output: "1"
Example 2:
Input: 4 Output: "1211"
//刚开始没能理解题意,然后看别人博客,原来是统计上一个字符串数字的个数
class Solution {
public:
string countAndSay(int n) {
string s = "1", e;
for(int i = 2; i <= n; i++)
{
int j = 0, len = s.length();
e = "";
while(j < len)
{
int k = j;
while(s[j] == s[k]) k++;
e += k-j+'0';
e += s[j];
j = k;
}
s = e;
}
return s;
}
};
39
.
Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7]
and target 7
,
A solution set is:
[ [7], [2, 2, 3] ]
//贼暴力的方法。。。
class Solution {
public:
void dfs(int i, int len, vector<int> &nums, int target, int sum, vector<int> x)
{
if(i == len)
{
if(sum == target) ans.push_back(x);
return ;
}
if(sum > target) return ;
dfs(i+1, len, nums, target, sum, x);
for(int j = 1; ; j++)
{
if(target < sum+nums[i]*j) break;
x.push_back(nums[i]);
dfs(i+1, len, nums, target, sum+nums[i]*j, x);
}
}
vector<vector<int>> ans;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> x;
dfs(0, candidates.size(), candidates, target, 0, x);
return ans;
}
};
40
.
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
//详见39题
class Solution {
public:
void dfs(int i, int len, vector<int> &nums, int target, int sum, vector<int> x)
{
if(i == len)
{
if(sum == target) ans.push_back(x);
return ;
}
if(sum > target) return ;
int l = i, r = i;
while(r < len && nums[l] == nums[r]) r++;
dfs(r, len, nums, target, sum, x);
for(int j = 1; j <= r - l; j++)
{
x.push_back(nums[i]);
dfs(r, len, nums, target, sum+nums[i]*j, x);
}
}
vector<vector<int>> ans;
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> x;
dfs(0, candidates.size(), candidates, target, 0, x);
return ans;
}
};