36. Valid Sudoku
Medium
781275FavoriteShare
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
Input: [ ["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"] ] Output: true
Example 2:
Input: [ ["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"] ] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
先上个弱鸡的写法:
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
//把需要对比的9个数字的x,y放到数组a中,用check函数对比
vector<int> a;
vector<vector<int>> b;
a.push_back(0);
a.push_back(0);
for(int i=0;i<9;i++)
{
b.push_back(a);
}
vector<vector<int>> z;//全零矩阵,用于归零
z=b;
//先检查行
for(int i=0;i<9;i++)
{
b=z;
for(int j=0;j<9;j++)
{
b[j][0]=i;
b[j][1]=j;
}
if(check(board,b))return false;
}
//再检查列
for(int i=0;i<9;i++)
{
b=z;
for(int j=0;j<9;j++)
{
b[j][0]=j;
b[j][1]=i;
}
if(check(board,b))return false;
}
//最后检查3*3格子
if(check(board,{{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}}))return false;
if(check(board,{{3,0},{3,1},{3,2},{4,0},{4,1},{4,2},{5,0},{5,1},{5,2}}))return false;
if(check(board,{{6,0},{6,1},{6,2},{7,0},{7,1},{7,2},{8,0},{8,1},{8,2}}))return false;
if(check(board,{{0,3},{0,4},{0,5},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5}}))return false;
if(check(board,{{3,3},{3,4},{3,5},{4,3},{4,4},{4,5},{5,3},{5,4},{5,5}}))return false;
if(check(board,{{6,3},{6,4},{6,5},{7,3},{7,4},{7,5},{8,3},{8,4},{8,5}}))return false;
if(check(board,{{0,6},{0,7},{0,8},{1,6},{1,7},{1,8},{2,6},{2,7},{2,8}}))return false;
if(check(board,{{3,6},{3,7},{3,8},{4,6},{4,7},{4,8},{5,6},{5,7},{5,8}}))return false;
if(check(board,{{6,6},{6,7},{6,8},{7,6},{7,7},{7,8},{8,6},{8,7},{8,8}}))return false;
return true;
}
bool check(vector<vector<char>>& board,vector<vector<int>> b)
{
vector<char> c;//用于查重的矩阵
vector<char>::iterator p;
c.clear();
for(int i=0;i<9;i++)
{
p=find(c.begin(),c.end(),board[(b[i][0])][(b[i][1])]);
if(board[b[i][0]][b[i][1]]=='.'){}
else if(p==c.end()){c.push_back(board[(b[i][0])][(b[i][1])]);}
else{return true;} //有重复的
}
return false;//没有重复的
}
};
因为写3*3小格子我有点混乱,我就直接上了。然后AC了,但看上去这种写法很弱鸡。而且最后结果显示我的程序比别人慢,占内存多:((。我决定要重新写一下。
Success
Runtime: 44 ms, faster than 7.34% of C++ online submissions for Valid Sudoku.
Memory Usage: 18.4 MB, less than 5.00% of C++ online submissions for Valid Sudoku.