【Valid Sudoku】cpp
题目:
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(std::vector<std::vector<char> > &board)
{
int len = board.size();
if (len != ) return false;
bool row_check[];
bool col_check[];
bool subBox_check[];
for (int row = ; row < len; ++row)
{
std::fill(row_check, row_check+, false);
std::fill(col_check, col_check+, false);
std::fill(subBox_check, subBox_check+, false);
for (int col = ; col < len; ++col)
{
if (!Solution::if_valid(board, row, col, row_check)){
return false;
}
if (!Solution::if_valid(board, col, row, col_check)){
return false;
}
if (!Solution::if_valid(board, *(row/)+col/, *(row%)+col%, subBox_check)){
return false;
}
}
}
return true;
}
static bool if_valid(std::vector<std::vector<char> > &board, int x, int y, bool (&some_check)[])
{
if (board[x][y]=='.'){
return true;
}
else{
if (some_check[board[x][y]-'']){
return false;
}
else{
some_check[board[x][y]-''] = true;
return true;
}
}
return false;
}
};
Tips:
1. 整体逻辑按照数独的要求走:
a. 判断同一行
b. 判断同一列
c. 判断所在的sub box
2. 判断时主要利用的数据结构是类似HashSet,由于三类判断条件类似,因此单独提出来一段代码。
3. 一点儿技巧:这里时间复杂度没有什么可说的就是O(n²);可以做些文章的地方就是代码复杂度,如何遍历一次for..for...就判断完成全部的逻辑。
a. 这里的核心就在于数独是对称的矩阵,行列对称;因此,行列坐标互换就可以走一次for...for...就完成1.a和1.b的判断逻辑。
b. 这里还需要注意的是,for..for..中走完一列,则需要判断一个sub box是否valid;因此,需要将某一列的坐标映射到某个sub box上。搞清楚这一点就可以推理处坐标变换的公式。
4. 在离线测试的时候,处理c++的 vector 二维字符数组还是非常不熟练,由Python转成cpp刷题确实不太习惯。后面在刷到string的题目时候,再把这cpp这部分强化一下。
==================================================
第二次过这道题,第一次没有AC还是错在了sub matrix小方块的检验上面。
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
for ( int i=; i<board.size(); ++i )
{
// check row
vector<bool> row_used(,false);
for ( int j=; j<board[i].size(); ++j )
{
if ( board[i][j]!='.' )
{
if ( row_used[board[i][j]-''] ) return false;
row_used[board[i][j]-''] = true;
}
}
// check col
vector<bool> col_used(,false);
for ( int j=; j<board.size(); ++j)
{
if ( board[j][i]!='.')
{
if ( col_used[board[j][i]-''] ) return false;
col_used[board[j][i]-''] = true;
}
}
// check sub matrix
vector<bool> subm_used(,false);
for ( int j=; j<board[i].size(); ++j )
{
int row = *(i/) + j/;
int col = *(i%) + j%;
if ( board[row][col]!='.' )
{
if ( subm_used[board[row][col]-''] ) return false;
subm_used[board[row][col]-''] = true;
}
}
}
return true;
}
};
tips:
这个要记住了
row = 3*(i/3) + j/3
col = 3*(i%3) + j%3
【Valid Sudoku】cpp的更多相关文章
- 【Valid Palindrome】cpp
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 【Valid Parentheses】cpp
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 【Valid Number】cpp
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Gray Code】cpp
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
随机推荐
- 返回mapcontrol上的已被选择的element
IGraphicsContainerSelect.SelectedElement
- [转] Adobe acrobat 破解教程
最新版的Adobe Acrobat DC Pro可以使我们更方便的管理和编辑PDF文档,现在我为大家带来Adobe Acrobat DC Pro安装及破解教程,供大家安装和使用. 工具/原料 Ad ...
- C#运算符、控制流
1 运算符 1.1 一元运算符: -(负号).+(正号):可以省略 1.2 二元运算符: 优先级,*(乘)./(除).%(取余).+(加).-(减).=(赋值) 二元赋值运算符,=.+=.-= ...
- zabbix文档3.4-7配置
zabbix文档3.4-7配置 1 主机和主机组 典型的Zabbix主机是您希望监视的设备(服务器,工作站,交换机等). 创建主机是Zabbix中首个监控任务之一.例如,如果要监视服务器"x ...
- LeetCode OJ 3Sum 3个整数之和
题意:给一个vector<int>容器,要求每当找到3个元素之和为0时就将这3个数按大小顺序记下来,用一个二维vector返回.也就是vector< vector<int> ...
- 知名nodeJS框架Express作者宣布弃nodeJS投Go
知名 nodeJS 框架 Express 的作者 TJ Holowaychuk 在 Twitter 发推并链接了自己的一篇文章,宣布弃 nodeJS 投 Go. 他给出的理由是:Go 语言和 Rust ...
- Codeforces Round #327 (Div. 2) C Median Smoothing(找规律)
分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...
- SqlServer 学习笔记
随机函数 select rand() declare @age int set @age = rand()*100 select @age 数据类型转换 declare @birthday datat ...
- TensorFlow 内置重要函数解析
概要 本部分介绍一些在 TensorFlow 中内置的重要函数,了解这些函数有时候更加方便我们进行数据的处理或者构建神经网络. 这些函数如下: tf.one_hot() tf.ra ...
- Bootstrap 附加导航(Affix)插件
bootstrap 附加导航(Affix)插件允许某个div元素固定到页面中的某个位置.您可以打开或关闭使用该插件之间进行切换 后续再写