LeetCode 036 Valid Sudoku
题目要求: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.
分析:
本题是让写数独的check函数(isValidSudoku)~基本有三个步骤:
① 每一行都合法;
② 每一列都合法;
③ 每一块都合法(3 * 3)。
代码如下:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return
isValidRow(board) && isValidColumn(board) && isValidBox(board);
} private: bool isValidRow(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[i][j])) {
return false;
}
}
}
return true;
} bool isValidColumn(vector<vector<char> > &board) {
int count[9];
for (int i = 0; i < 9; i++) {
memset(count, 0, sizeof(int) * 9);
for (int j = 0; j < 9; j++) {
if (!add(count, board[j][i])) {
return false;
}
}
}
return true;
} bool isValidBox(vector<vector<char> > &board) {
int point[9][2] = {
{1, 1}, {1, 4}, {1, 7}, {4, 1}, {4, 4}, {4, 7}, {7, 1}, {7, 4}, {7, 7}
};
int dir[8][2] = {
{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}
};
int count[10]; for (int i = 0, x, y; i < 9; i++) {
memset(count, 0, sizeof(int) * 10);
x = point[i][0];
y = point[i][1];
add(count, board[x][y]);
for (int j = 0; j < 8; j++) {
if (!add(count, board[dir[j][0] + x][dir[j][1] + y])) {
return false;
}
}
}
return true;
} bool add(int count[], char c) {
if (c == '.') {
return true;
} //如果每个字符出现次数 <= 1,则正常+_+
return (++count[c - '1']) <= 1;
}
};
简化版本:
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
// Note: The Solution object is instantiated only once.
vector<vector<bool>> rows(9, vector<bool>(9,false));
vector<vector<bool>> cols(9, vector<bool>(9,false));
vector<vector<bool>> blocks(9, vector<bool>(9,false)); for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
if(board[i][j] == '.')continue; //rows, cols, blocks分别是9个布尔值
//将每个点分别赋值为true
//若未赋值的为true了,则有问题
int num = board[i][j] - '1';
if(rows[i][num] || cols[j][num] || blocks[i - i%3 + j/3][num])
return false;
rows[i][num] = cols[j][num] = blocks[i - i%3 + j/3][num] = true;
}
return true;
} };
LeetCode 036 Valid Sudoku的更多相关文章
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- Java for LeetCode 036 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- 【LeetCode】036. Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- LeetCode(38)-Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- [leetcode] 20. Valid Sudoku
这道题目被放在的简单的类别里是有原因的,题目如下: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. ...
随机推荐
- java中常见的六种线程池详解
之前我们介绍了线程池的四种拒绝策略,了解了线程池参数的含义,那么今天我们来聊聊Java 中常见的几种线程池,以及在jdk7 加入的 ForkJoin 新型线程池 首先我们列出Java 中的六种线程池如 ...
- Deployment YAML方式创建Nginx服务
replication controller Replication Controller为Kubernetes的一个核心内容,应用托管到Kubernetes之后,需要保证应用能够持续的运行,Repl ...
- PASS模型-第一周个人报告
PASS模型-第一周个人报告 博客班级 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus ...
- uniapp微信小程序canvas隐藏
HTML 我是把canvas嵌套在view里并被view设置id <view id="canvas"> <canvas style="width: 35 ...
- opencv--ORB::create
static Ptr<ORB> cv::ORB::create ( int nfeatures = 500, ...
- Flink的sink实战之四:自定义
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 【QT】 Qt多线程的“那些事”
目录 一.前言 二.QThread源码浅析 2.1 QThread类的定义源码 2.2 QThread::start()源码 2.3 QThreadPrivate::start()源码 2.4 QTh ...
- 解决 cannot resolve 依赖包的问题
在maven import的时候 报这样的错误 之前也经常碰到这样的错误,通过reimport.清缓存等方法都可以解决.但这次试了好多次都还是这样,查看maven后发现我pom文件里也没写错. 最后是 ...
- VirtualBox上桥接方式安装CentOS之后借助宿主上网
VirtualBox上桥接方式安装CentOS之后借助宿主上网: 修改centos文件: 1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 增加以下几项: ...
- jdk+tomcat 文件下载
1.下载jdk+tomcat 链接:https://pan.baidu.com/s/1DQ-l2S4th9BoucWqAymmLg :密码: zdd3 备:tomcat是解压包,直接解压就能用,但需配 ...