当前位置:网站首页>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;
}边栏推荐
- Work accumulation - judge whether GPS is on
- Pat class B 1023 minimum decimals
- 内存分析与内存泄漏检测
- 【Leetcode】431. Encode n-ary tree to binary tree (difficult)
- 使用aggregation API扩展你的kubernetes API
- Efficient office of fintech (I): automatic generation of trust plan specification
- About the error of installing PIP3 install chatterbot
- How to add libraries for Arduino ide installation
- Activity startup mode and life cycle measurement results
- Cryptography series: certificate format representation of PKI X.509
猜你喜欢
随机推荐
Pyinstaller package exe setting icon is not displayed
[cocos2d-x] custom ring menu
Ant Usage Summary (III): batch packaging apk
Paper notes: multi label learning lsml
Wireshark TS | 视频 APP 无法播放问题
Kotlin interface
Day_12 传智健康项目-JasperReports
Pat class B 1021 digit statistics
mongodb 4. X binding multiple IP startup errors
Remove duplicates from sorted list II of leetcode topic resolution
【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
Pat class B 1018 C language
Pyqt5 设置窗口左上角图标
【Cocos2d-x】可擦除的Layer:ErasableLayer
基于T5L1的小型PLC设计方案
Layer 2技术方案进展情况
How to batch produce QR codes that can be read online after scanning
How to add libraries for Arduino ide installation
jvm-05.垃圾回收








