Valid Sudoku
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.
验证已经填好的数独是否合符规则。
思路:
行,列和小九宫分别检查就可以了。
有填好数字的就检查,没填写的可以不管。
但是也可以一起同时检查,时间效率稍微快一点,不过需要额外空间。
下面是分别检查行列和小九宫的程序:
class Solution {
public:
static const int SQUARENUM = 9;
static const int LITTLESQU = 3;
bool isValidSudoku(vector<vector<char> > &board)
{
vector<char> vChar(SQUARENUM);
for (int i = 0; i < SQUARENUM; i++)
if (!rowValid(board[i])) return false;
return colValid(board) && squValid(board);
}
bool rowValid(vector<char> &vChar)
{
vector<bool> nine(SQUARENUM+1, 0);
for (int i = 0; i < SQUARENUM; i++)
{
if (vChar[i] != '.')
{
int t = vChar[i] - '0';
if (nine[t])
return false;
else
nine[t] = 1;
}
}
return true;
}
bool colValid(vector<vector<char> > &board)
{
vector<bool> nine(SQUARENUM+1,0);
for (int i = 0; i < SQUARENUM; i++)
{
for (int j = 0; j < SQUARENUM; j++)
{
if (board[j][i]!= '.')
{
int t = board[j][i] - '0';
if (nine[t])
return false;
else
nine[t] = 1;
}
}
nine.clear();
nine.resize(SQUARENUM+1, 0);
}
return true;
}
bool squValid(vector<vector<char> > &board)
{
vector<bool> nine(SQUARENUM+1, 0);
for (int i = 0; i < SQUARENUM; i++)
{
for (int j = 0; j < SQUARENUM; j++)
{
int row = j/LITTLESQU + i/LITTLESQU*LITTLESQU,
col = j%LITTLESQU + i%LITTLESQU*LITTLESQU;
if (board[row][col]!= '.')
{
int t = board[row][col] - '0';
if (nine[t])
return false;
else
nine[t] = 1;
}
}
nine.clear();
nine.resize(SQUARENUM+1, 0);
}
return true;
}
};
下面是leetcode上的,以空间的代价换取代码更加简洁,很好的思路:
http://discuss.leetcode.com/questions/215/valid-sudoku
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board)
{
vector<vector<bool> > rows(9, vector<bool>(9, false));
vector<vector<bool> > cols(9, vector<bool>(9, false));
vector<vector<bool> > blocks(9, vector<bool>(9, false));
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] == '.') continue;
int c = board[i][j] - '1';
if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c])
return false;
rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true;
}
}
return true;
}
};
省空间的简洁代码:
//2014-1-26
bool isValidSudoku(vector<vector<char> > &board)
{
vector<bool> row_table(9);
vector<bool> col_table(9);
vector<bool> squ_table(9);
for (int i = 0; i < 9; i++)
{
row_table.clear(); row_table.resize(9);
col_table.clear(); col_table.resize(9);
squ_table.clear(); squ_table.resize(9);
for (int j = 0; j < 9; j++)
{
if (board[i][j] != '.')
{
int r = board[i][j] - '1';
if (!row_table[r]) row_table[r] = true;
else return false;
}
if (board[j][i] != '.')
{
int c = board[j][i] - '1';
if (!col_table[c]) col_table[c] = true;
else return false;
}
int r = i/3*3+j/3, c = i%3*3+j%3;
if (board[r][c] != '.')
{
int sq = board[r][c] - '1';
if (!squ_table[sq]) squ_table[sq] = true;
else return false;
}
}
}
return true;
}
分享到:
相关推荐
LeetCode Valid Parenthese解决方案
leetcode 答案 Sudoku ref: UserInterface IUserInterfaceContract:前端与后端的交互接口 EventListener:处理每次输入时的接口 View:更新前端以及消息提示的接口 SudokuTextField:继承了JavaFX中的TextField接口...
c++ c++_c++编程基础之leetcode题解第36题有效的数独
LeetCode_0098_验证二叉搜索树(中等, 2022-03)问题简述给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。98. 验证二
噪声 leetcode
python python_leetcode面试题解之第36题有效的数独_python题解
leetcode,刷题的一些解法,建议先自己写,然后再参考别人的思路, leetcode,刷题的一些解法,建议先自己写,然后再参考别人的思路,
36. 有效的数独概要这道题的解决方案有很多种,由于数组格式和长度都是固定的,我看到有些神仙甚至写死小宫格的坐标来答题...,题目的要求是行/列/3*3小宫格没
c++ c++_c++编程基础之leetcode题解第37题解数独
leetcode 71 Python用 Python 编写 Leetcode (数据科学家的解决方案) - 易于理解的最佳解决方案,可以通过所有 Leetcode 测试用例,对于非 SDE。 (只有简单和中等的问题) leetcode 10 - 正则表达式匹配(难) ...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
leetcode数独题
leetcode 答案使用计算机视觉和回溯的数独求解器 安装 存储库可以克隆或下载为 zip。 按照说明安装tesseract 其他需求可以通过运行pip install -r requirements.txt来安装 用法 执行代码如下: python main . py '...
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
java java_leetcode面试题解哈希表第36题有效的数独_题解
leetcode中文版
python python_leetcode面试题解之第37题解数独_python题解
vs code LeetCode 插件
sudoku example 做leetcode37的时候看到的解数独的非常棒的方法。在简单回溯的基础上还加上了限制条件,会更快。主要参考 . 自己在main里面加上了一点从txt里读写,这样可以自己敲进去题目然后得到答案,比只是...
大佬的leetcode刷题笔记(c++版本)