36. 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 '.'.

Note: A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

1. 每行必须由1~9组成，且不重复
2. 每列必须由1~9组成，且不重复
3. 每个3*3的九宫格必须由1~9组成，且不重复

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool rowValid[9][9]={false},colValid[9][9]={false},subBoardValid[9][9]={false};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
int num=board[i][j]-'0'-1;
int k=i/3*3+j/3;/*如果第一格九宫格的编号为0,board[i][j]这个元素存在于第(i/3*3+j/3)个九宫格内*/
if(rowValid[i][num]||colValid[j][num]||subBoardValid[k][num])/*如果在未赋值之前就为true,则说明发生了重复*/
return false;
rowValid[i][num]=colValid[j][num]=subBoardValid[k][num]=true;/*如果未重复，则将其赋值为true*/
}
}
}
return true;
}
};


学籍管理系统文档

北邮教务系统评教脚本

Published on September 17, 2017

72.Edit distance

Published on September 17, 2017