当前位置:网站首页>Leetcode topic resolution valid Sudoku
Leetcode topic resolution valid Sudoku
2022-06-23 06:17:00 【ruochen】
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 rows separately 、 Column 、3*3 square .
public boolean isValidSudoku(char[][] board) {
if (board == null || board.length != 9 || board[0].length != 9) {
return false;
}
int mx = board.length;
int my = board[0].length;
// row
for (int x = 0; x < mx; x++) {
boolean[] flag = new boolean[10];
for (int y = 0; y < my; y++) {
char c = board[x][y];
if (c != '.') {
if (flag[c - '0'] == false) {
flag[c - '0'] = true;
} else {
return false;
}
}
}
}
// column
for (int y = 0; y < my; y++) {
boolean[] flag = new boolean[10];
for (int x = 0; x < mx; x++) {
char c = board[x][y];
if (c != '.') {
if (flag[c - '0'] == false) {
flag[c - '0'] = true;
} else {
return false;
}
}
}
}
// square
for (int x = 0; x < mx / 3; x++) {
for (int y = 0; y < my / 3; y++) {
boolean[] flag = new boolean[10];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char c = board[x * 3 + i][y * 3 + j];
if (c != '.') {
if (flag[c - '0'] == false) {
flag[c - '0'] = true;
} else {
return false;
}
}
}
}
}
}
return true;
}边栏推荐
- Explicability of counter attack based on optimal transmission theory
- 关于安装pip3 install chatterbot报错的问题
- 密码学系列之:PKI的证书格式表示X.509
- Basic calculator II for leetcode topic analysis
- The hierarchyviewer tool cannot find the hierarchyviewer location
- Operating mongodb in node
- Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
- Gplearn appears assignment destination is read only
- SSM project construction
- Network packet capturing tcpdump User Guide
猜你喜欢

Difference between MySQL read committed and repeatability

Day_11 传智健康项目-图形报表、POI报表

【Vivado那些事儿】XilinxCEDStore介绍

jvm-06. Garbage collector

jvm-03. JVM memory model

Activity startup mode and life cycle measurement results

十一、纺织面料下架功能的实现

ant使用总结(二):相关命令说明

又到半年总结时,IT人只想躺平

Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
随机推荐
Simple about fastdfs
mysql读已提交和可重复度区别
关于安装pip3 install chatterbot报错的问题
[cocos2d-x] custom ring menu
(1) Basic learning - Common shortcut commands of vim editor
Infotnews | which Postcard will you receive from the universe?
Adnroid activity screenshot save display to album view display picture animation disappear
Gplearn appears assignment destination is read only
Visual Studio调试技巧
论文笔记: 多标签学习 LSML
jvm-03.jvm内存模型
JS interview question - anti shake function
Memory analysis and memory leak detection
Global attribute lang attribute
Ant Usage Summary (II): description of related commands
Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
[vivado] xilinxcedstore introduction
[focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
Basic calculator II for leetcode topic analysis
Remove the influence of firewall and virtual machine on live555 startup IP address