【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. ...
随机推荐
- mysql数据库字段类型的选择原则
原文链接:http://blog.csdn.net/u013412790/article/details/51615407 数据库类型的选择对数据库的性能影响很大 1 . 数据类型会影响存储空间的开销 ...
- AngularJS 整理学习
参考博客: https://blog.csdn.net/weixin_33178524/article/details/79179597 https://blog.csdn.net/qq_42128 ...
- 网页游戏中PK系统的实现
在游戏开发过程中,写过一个简单的PK系统面板,涉及到前端和后端的交互,我将自己制作的流程分享给大家,大概流程是这样:前端发送PK邀请给后端,后端受到请求后将信息返回给前端处理,先来看下整个流程图及思路 ...
- Altera FFT核使用详解
简介 快速傅里叶变换(Fast Fourier Transform)最为一种高效的算法,被广泛的用于信号处理与数据分析等领域.对于设计工程师来讲,自己动手采样可编程语言来实现一个FFT/IFFT模块, ...
- [Asp.Net] web api 部署注意事项
在将web api项目部署到IIS上的时候 要将应用程序池设置成.net framework 4.0版本
- SQL Server 删除当前数据库中所有数据库 ,无视约束
Sql Server中清空所有数据表中的记录 清空所有数据表中的记录: exec sp_msforeachtable @Command1 ='truncate table ?' 删除所有数据表: e ...
- linux 命令——27 chmod
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法. 一种是包含字母和操作符表达式的文字设定法: 另一种是包含数字的数字设定法. Linux系统中 ...
- innobackupex基于binlog日志的恢复 -- 使用mysqlbinlog恢复
备份先做一次完整备份: innobackupex --defaults-file=/etc/my.cnf --user root --password chengce243 /data/mysqlba ...
- 【CF1000C】Covered Points Count(离散化+差分)
点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...
- 【BZOJ1030】[JSOI2007] 文本生成器(AC自动机上跑DP)
点此看题面 大致题意: 给你\(N\)个字符串(只含大写字母),要你求出有多少个由\(M\)个大写字母构成的字符串含有这\(N\)个字符串中的至少一个. \(AC\)自动机 看到题目,应该比较容易想到 ...