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. ...
随机推荐
- 【Spring Boot】web开发
springboot在开发web项目的时候具备天然的优势,现在的很多企业级开发都是依托于springboot的. 使用springboot的步骤: 1.创建一个SpringBoot应用,选 ...
- python的各版本的不同
Python的版本主要分为 2.× . 3.× 两个系列. Python3计划每年发布一个新的子版本,一次只增加一两种新语法. 使用时当然选择越新的Python版本越好,版本越老的代码越难维护. 维护 ...
- Pycharm激活码(2020最新永久激活码)
如果下边的Pycharm激活码过期失效了的话,大家可以关注我的微信公众号:Python联盟,然后回复"激活码"即可获取最新Pycharm永久激活码! 56NPDDVEIV-eyJs ...
- Azure Data Factory(四)集成 Logic App 的邮件通知提醒
一,引言 上一篇有介绍到使用Azure Data Factory 复制数据,然后有集成 Azure DevOps 实现CI/CD,但是对于真正的项目来说,这些肯定是不够的,比如说在执行 Azure P ...
- 一次webapi Post请求失败记录
//点击添加按钮 $("#add").click(function () { //layer.msg("添加"); var data = { "stu ...
- vim编辑器的常用命令
按ESC键跳到命令模式,然后::w - 保存文件,不退出 vim.:w file -将修改另外保存到 file 中,不退出 vim.:w! -强制保存,不退出 vim .:wq -保存文件,退出 vi ...
- 2Git分支问题
1,查看所有分支: git branch *号在哪表明当前分支在哪. 2,新建一个分支: git branch featureq(分支名) 转到该分支下: git checkout featureq ...
- 人体动作捕捉格式之BVH
BVH简介 BVH是BioVision公司推出的一种人体动作捕捉文件格式.这种文件以节点为核心元素,记录连续数帧内人体骨架的运动. BVH=? 研究一个东西的时候我比较喜欢先研究它的名字.BVH可以认 ...
- Python_爬虫_BeautifulSoup网页解析库
BeautifulSoup网页解析库 from bs4 import BeautifulSoup 0.BeautifulSoup网页解析库包含 的 几个解析器 Python标准库[主要,系统自带;] ...
- 你真的能分清putc()函数和fputc()函数吗?
最近小编在讲关于C语言文件部分内容,讲到了fputc()函数.前面在讲字符串的时候,我们还学习过putc()函数,我们知道,putc()函数一般用于标准输出,fputc()函数一般用于文件读写.刚才呢 ...