#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
bool array[10] = { 0 };
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
return judge_rows(board) && judge_cols(board) && judge_palaces(board);
}
bool judge_rows(const vector<vector<char>>& board) {
int size = board.size();
for (int row = 0;row < size;row++) {
const vector<char>&row_board = board[row];
memset(array, 0, sizeof(array));
for (auto &ch : row_board) {
if ('.' == ch) {
continue;
}
int index = ch - '0';
if (!array[index]) {
array[index] = true;
}
else {
return false;
}
}
}
return true;
}
bool judge_cols(const vector<vector<char>>& board) {
int size = board.size();
for (int col = 0;col < size;col++) {
memset(array, 0, sizeof(array));
for (int i = 0;i < size;i++) {
char ch = board[i][col];
if ('.' == ch) {
continue;
}
int index = ch - '0';
if (!array[index]) {
array[index] = true;
}
else {
return false;
}
}
}
return true;
}
bool judge_palaces(const vector<vector<char>>& board) {
int size = board.size();
for (int row = 0;row < size;row +=3) {
for (int col = 0;col < size;col +=3) {
memset(array, 0, sizeof(array));
for (int i = row;i < row + 3;i++) {
for (int j = col;j < col + 3;j++) {
char ch = board[i][j];
if ('.' == ch) {
continue;
}
int index = ch - '0';
if (!array[index]) {
array[index] = true;
}
else {
return false;
}
}
}
}
}
return true;
}
};
int main() {
/* vector<vector<char>>board = { {'5','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}
};*/
vector<vector<char>>board = { {'8','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}
};
Solution ss;
std::cout << ss.isValidSudoku(board) << std::endl;
return 0;
}
leetcode 有效的数独
原创
©著作权归作者所有:来自51CTO博客作者我是006的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:常见openssl加解密命令
下一篇:snort检测插件初始化流程
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【LeetCode】39. 组合总和
【LeetCode】39. 组合总和
leetcode 数据结构与算法 C++ -
【LeetCode】705. 设计哈希集合
【LeetCode】705. 设计哈希集合
leetcode 数据结构与算法 C++ -
【LeetCode 36】有效的数独
"题目链接" 【题解】 就一傻逼模拟题 【代码】
模拟 LeetCode i++ 编程题目 -
Java实现 LeetCode 36 有效的数独
36. 有效的数独判断一个 9x9 的数独是...
数独 商业 实线 i++ JAVA