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.

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

分析:检验数独是否有效。只用检查已经填充的部分,没填充的部分用 '.' 表示。

分别检查行、列以及小九宫格。为了避免麻烦,我用了map来迅速查找数字。运行时间46ms。

 

Valid Sudoku
 1 class Solution {

 2 public:

 3     bool isValidSudoku(vector<vector<char>>& board) {

 4         for(int i = 0; i < 9; i++){

 5             //check each row

 6             map<char, int> m;

 7             for(int j = 0; j < 9; j++){

 8                 if(board[i][j] != '.'){

 9                     if(m.find(board[i][j]) != m.end()) return false;

10                     else m[board[i][j]] = 1;

11                 }

12             }

13             

14             //check each column

15             map<char, int> n;

16             for(int k = 0; k < 9; k++){

17                 if(board[k][i] != '.'){

18                     if(n.find(board[k][i]) != n.end()) return false;

19                     else n[board[k][i]] = 1;

20                 }

21             }

22         }

23         //check each sub-boxes

24         for(int i = 0; i < 9; i += 3){

25             for(int j = 0; j < 9; j += 3){

26                 map<char, int> mm;

27                 for(int p = i; p < i + 3; p++){

28                     for(int q = j; q < j + 3; q++){

29                         if(board[p][q] != '.'){

30                             if(mm.find(board[p][q]) != mm.end()) return false;

31                             else{

32                                 mm[board[p][q]] = 1;

33                             }

34                         }

35                     }

36                 }

37             }

38         }

39         return true;

40     }

41 };
View Code

 

 

 为了进一步优化,可以不用map进行数字重复测试。通过使用一个bool型数组,每出现一个数字,便将该数字相应下标置1,运行时间16ms!

 1 class Solution {

 2 public:

 3     bool isValidSudoku(vector<vector<char>>& board) {

 4         for(int i = 0; i < 9; i++){

 5             //check each row

 6             bool arrRow[9];

 7             fill(arrRow, arrRow + 9, false);

 8             for(int j = 0; j < 9; j++){

 9                 if(board[i][j] != '.'){

10                     if(arrRow[board[i][j] - '1']) return false;

11                     else arrRow[board[i][j] - '1'] = true;

12                 }

13             }

14             

15             //check each column

16             bool arrCol[9];

17             fill(arrCol, arrCol + 9, false);

18             for(int k = 0; k < 9; k++){

19                 if(board[k][i] != '.'){

20                     if(arrCol[board[k][i] - '1']) return false;

21                     else arrCol[board[k][i] - '1'] = true;

22                 }

23             }

24         }

25         //check each sub-boxes

26         for(int i = 0; i < 9; i += 3){

27             for(int j = 0; j < 9; j += 3){

28                 bool arr[9];

29                 fill(arr, arr + 9, false);

30                 for(int p = i; p < i + 3; p++){

31                     for(int q = j; q < j + 3; q++){

32                         if(board[p][q] != '.'){

33                             if(arr[board[p][q] - '1']) return false;

34                             else arr[board[p][q] - '1'] = true;

35                         }

36                     }

37                 }

38             }

39         }

40         return true;

41     }

42 };

 

你可能感兴趣的:(sudo)