当前位置:网站首页>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;
}边栏推荐
- [DaVinci developer topic] -42- how to generate template and header files of APP SWC
- Day_08 传智健康项目-移动端开发-体检预约
- WordPress aawp 3.16 cross site scripting
- JS interview question - anti shake function
- 【Cocos2d-x】自定义环形菜单
- 求二叉树最宽的层有多少个节点
- Pat class B 1025 reverse linked list
- Pat class B 1023 minimum decimals
- The hierarchyviewer tool cannot find the hierarchyviewer location
- 又到半年总结时,IT人只想躺平
猜你喜欢

Infotnews | which Postcard will you receive from the universe?

jvm-01. Instruction rearrangement
![[open source project] excel export Lua configuration table tool](/img/3a/8e831c4216494d5497928bae21523b.png)
[open source project] excel export Lua configuration table tool

Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library

Redis 哨兵

Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)

微软面试题:打印折纸的折痕

Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql

Day_09 传智健康项目-移动端开发-手机快速登录、权限控制

mongodb 4.x绑定多个ip启动报错
随机推荐
Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
Pyinstaller package exe setting icon is not displayed
matplotlib savefig多个图片叠加问题
Layer 2技术方案进展情况
Day_13 传智健康项目-第13章
Vite learning (I) - Introduction
mongodb 4.x绑定多个ip启动报错
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
【Cocos2d-x】可擦除的Layer:ErasableLayer
Summary of ant usage (I): using ant to automatically package apk
Perfect squares for leetcode topic analysis
Sorting out common problems after crawler deployment
Pat class B 1026 program running time
Network packet capturing tcpdump User Guide
SQL statement error caused by the same SQL table name and function name.
【DaVinci Developer专题】-41-APP SWC如何读取写入NVM Block数据
gplearn出现 assignment destination is read-only
Cloud native database is the future
Infotnews | which Postcard will you receive from the universe?