【LeetCode】036. 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.
题解:
Solution 1
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ;
for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[i][j], used))
return false;
}
}
for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[j][i], used))
return false;
}
}
for(int r = ; r < ; ++r){
for(int c = ; c < ; ++c){
bool used[n] = {false};
for(int i = r * ; i < r * + ; ++i){
for(int j = c * ; j < c * + ; ++j){
if(isused(board[i][j], used))
return false;
}
}
}
}
return true;
}
bool isused(char val, bool used[]){
if(val == '.')
return false;
if(used[val - ''])
return true;
used[val - ''] = true;
return false;
}
};
Solution 2
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ;
vector<vector<bool>> rowboard(n, vector<bool>(n, false));
vector<vector<bool>> colboard(n, vector<bool>(n, false));
vector<vector<bool>> celboard(n, vector<bool>(n, false));
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
if(board[i][j] == '.') continue;
int ch = board[i][j] - '';
if(rowboard[i][ch] || colboard[ch][j] || celboard[ * (i / ) + j / ][ch])
return false;
rowboard[i][ch] = true;
colboard[ch][j] = true;
celboard[ * (i / ) + j / ][ch] = true;
}
}
return true;
}
};
【LeetCode】036. Valid Sudoku的更多相关文章
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】36 - Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...
- 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)
Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
- 【一天一道LeetCode】#36. Valid Sudoku
一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...
- 【LeetCode】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Vue学习-基础语法
Vue v-if指令 Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTM ...
- OpenCV编程->RGB直方图统计
我们在处理彩色图像时.特别是在做局部图像的阈值切割时,须要一个直观的RGB统计图. 接下来開始实现. 代码: void CalcHistRGB() { IplImage* img_sou ...
- Oracle视图传递参数
在Oracle里,视图不像存储过程和函数一样,可以定义输入参数,但我们可以变个方式,使用程序包来实现. oracle package: oracle package是oracle包,是一组相关过程.函 ...
- Struts详解
1.什么是MVC? MVC是Model,View,Controller的缩写,MVC是Application开发的设计模式, 也就是大家所知道的Model2.在MVC的设计模式中,它包括三类对象:(1 ...
- 查看django的安装路径
查看django的安装路径 pip3 show django
- KinedEditor特性
谷歌浏览器会将kindeditor在其他js文件加载完之后加载 kindeditor是异步加载,document.ready完了,kindeditor可能还没加载完 kind会将选区变成一个节点 1. ...
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ #include <iostream> #in ...
- mini2440移植uboot 2014.04(一)
最新版的uboot添加了很多新功能,我决定在最新版代码基础上重新移植一遍加深理解. 我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot- ...
- Vim 命令记录与回放
步骤如下: q+(a..z)寄存器名: 执行你要执行的操作: q 结束操作: 调用为@+寄存器: 列子如下: 在写PHP 程序时用的比较多的是创建函数: 如 function add_in(){ } ...
- hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】
题目地址:http://hihocoder.com/contest/hiho57/problem/1 输入 第1..5行:1个长度为6的字符串,表示该行的格子状态,1表示该格子是亮着的,0表示该格子是 ...