https://leetcode.com/problems/valid-sudoku/
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is 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.
classSolution { Public: Vector<int> Getidx (intIintj) {Vector<int> idx (4); intRow, col; if(i>=0&& i<=2) {idx[0] =0; idx[1] =2; } Else if(i>=3&& i<=5) {idx[0] =3; idx[1] =5; } Else if(i>=6&& i<=8) {idx[0] =6; idx[1] =8; } if(j>=0&& j<=2) {idx[2] =0; idx[3] =2; } Else if(j>=3&& j<=5) {idx[2] =3; idx[3] =5; } Else if(j>=6&& j<=8) {idx[2] =6; idx[3] =8; } returnidx; } BOOLCheckrowandcolumn (vector<vector<Char>>& Board,intIintj) {if(i<0|| I>=board.size () | | j<0|| j>=board[0].size ())return false; for(intNi=0; Ni<board.size (); + +ni) { if(ni = = I | | board[ni][j] = ='.')Continue; if(Board[ni][j] = = Board[i][j])return false; } for(intnj=0; nj<board[0].size (); + +NJ) { if(NJ = = J | | board[i][nj] = ='.')Continue; if(Board[i][nj] = = Board[i][j])return false; } return true; } BOOLChecklocal (vector<vector<Char>>& Board,intIintj) {if(i<0|| I>=board.size () | | j<0|| j>=board[0].size ())return false; Vector<int> idx =Getidx (i, j); intLi = idx[0], RI = idx[1], LJ = idx[2], RJ = idx[3]; for(intp=li;p<=ri;++p) { for(intq=lj;q<=rj;++q) {if((i==p && j==q) | | board[p][q] = ='.')Continue; if(Board[i][j] = = Board[p][q])return false; } } return true; } BOOLIsvalidsudoku (vector<vector<Char>>&Board) { for(intI=0; I<board.size (); + +i) { for(intj=0; J<board[i].size (); + +j) {if(Board[i][j] = ='.')Continue; if(!checklocal (board, I, j) | |!checkrowandcolumn (BOARD, I, J))return false; } } return true; }};
View Code
[email protected] [approx] Valid Sudoku