【LeetCode】036. Valid Sudoku
2024-08-24 20:11:35
题目:
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.
题解:
Solution 1
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ; for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[i][j], used))
return false;
}
}
for(int i = ; i < n; ++i){
bool used[n] = {false};
for(int j = ; j < n; ++j){
if(isused(board[j][i], used))
return false;
}
}
for(int r = ; r < ; ++r){
for(int c = ; c < ; ++c){
bool used[n] = {false};
for(int i = r * ; i < r * + ; ++i){
for(int j = c * ; j < c * + ; ++j){
if(isused(board[i][j], used))
return false;
}
}
}
}
return true;
}
bool isused(char val, bool used[]){
if(val == '.')
return false;
if(used[val - ''])
return true;
used[val - ''] = true;
return false;
}
};
Solution 2
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int n = ;
vector<vector<bool>> rowboard(n, vector<bool>(n, false));
vector<vector<bool>> colboard(n, vector<bool>(n, false));
vector<vector<bool>> celboard(n, vector<bool>(n, false));
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
if(board[i][j] == '.') continue;
int ch = board[i][j] - '';
if(rowboard[i][ch] || colboard[ch][j] || celboard[ * (i / ) + j / ][ch])
return false;
rowboard[i][ch] = true;
colboard[ch][j] = true;
celboard[ * (i / ) + j / ][ch] = true;
}
}
return true;
}
};
最新文章
- Linux下安装性能测试负载机LG
- Java:对象的强、软、弱和虚引用
- 众安「尊享e生」果真牛的不可一世么?
- php 工厂模式
- nsstring字符串重组
- YUI Reset CSS (学习摘抄)
- Laravel 安装predis 扩展
- java环境中基于jvm的两大语言:scala,groovy
- 从源码看java中Integer的缓存问题
- PHP得出附件扩展名
- A题进行时--浙大PAT 1001-1010
- sap判断条件
- ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
- 关于node的基础理论,书上看来的
- Java开发笔记(六十七)清单:ArrayList和LinkedList
- .net 委托多线程 实时更新界面
- pythonのdjango CSRF简单使用
- ubuntu14.04安装opencv3.1
- Solidity-让合约地址 接受ETH的转账充值的 三种方式
- 【转】【JLINK下载失败,STLINK下载失败万能解决方案】JLINK和STLINK都无法下载时的解决办法,此时芯片并没有报废