leetcode36. 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 '.'
.
题目意思就是判断是否符合数独的要求,即行内无重复,列内无重复,九个小的正方形也没有重复。
1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char>>& board) { 4 int validRow[9][9] = {0},validCol[9][9] = {0}, validSu[9][9] = {0}; 5 6 for( int i = 0; i < board.size() ; ++i) 7 { 8 for( int j = 0; j < board[i].size() ; ++j) 9 { 10 if( board[i][j] != '.') 11 { 12 int num = board[i][j] - '0' -1; 13 int k = i/3 *3 +j/3; 14 if(validRow[i][num] || validCol[j][num]||validSu[k][num]) 15 return false; 16 else 17 validRow[i][num] = validCol[j][num] = validSu[k][num] = 1; 18 } 19 } 20 } 21 return true; 22 23 } 24 };