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.

 

 1 public class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(board == null || board.length != 9 || board[0].length != 9) return false;
 5         int[] checkr = new int[9];
 6         int[] checkc = new int[9];
 7         for(int i = 0; i < 9; i ++){
 8             for(int j = 0; j < 9; j ++){
 9                 if(board[i][j] - '0' > 0 && board[i][j] - '0' < 10){
10                     checkr[board[i][j] - '0' - 1] ++;
11                 }
12                 if(board[j][i] - '0' > 0 && board[j][i] - '0' < 10){
13                     checkc[board[j][i] - '0' - 1] ++;
14                 }
15             }
16             for(int ii = 0; ii < 9; ii ++){
17                 if(checkr[ii] > 1 || checkc[ii] > 1){
18                     return false;
19                 }
20             }
21             checkr = new int[9];
22             checkc = new int[9];
23         }
24         for(int i = 0; i < 9; i ++){
25             for(int j = 0; j < 3; j ++){
26                 if(board[i][j] != '.')
27                     checkc[board[i][j] - '0' - 1] ++;
28             }
29             if((i + 1) % 3 == 0){
30                 for(int ii = 0; ii < 9; ii ++){
31                     if(checkr[ii] > 1 || checkc[ii] > 1){
32                         return false;
33                     }
34                 }
35                 checkc = new int[9];
36                 }
37             }
38         
39         checkc = new int[9];
40         for(int i = 0; i < 9; i ++){
41             for(int j = 3; j < 6; j ++){
42                 if(board[i][j] != '.')
43                     checkc[board[i][j] - '0' - 1] ++;
44             }
45             if((i + 1) % 3 == 0){
46                 for(int ii = 0; ii < 9; ii ++){
47                     if(checkr[ii] > 1 || checkc[ii] > 1){
48                         return false;
49                     }
50                 }
51                 checkc = new int[9];
52                 }
53             }
54         
55         checkc = new int[9];
56         for(int i = 0; i < 9; i ++){
57             for(int j = 6; j < 9; j ++){
58                 if(board[i][j] != '.')
59                     checkc[board[i][j] - '0' - 1] ++;
60             }
61             if((i + 1) % 3 == 0){
62                 for(int ii = 0; ii < 9; ii ++){
63                     if(checkr[ii] > 1 || checkc[ii] > 1){
64                         return false;
65                     }
66                 }
67                 checkc = new int[9];
68                 }
69             }
70         
71         return true;
72     }
73 }

 第二遍:

 1 public class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(board == null || board.length != 9 || board[0].length != 9) return false;
 5         boolean[] checkr = new boolean[9];
 6         boolean[] checkc = new boolean[9];
 7         for(int i = 0; i < 9; i ++){
 8             checkr = new boolean[9];
 9             checkc = new boolean[9];
10             for(int j = 0; j < 9; j ++){
11                 if ( board[i][j] != '.'){
12                     if(checkr[board[i][j] - '1']) return false;
13                     checkr[board[i][j] - '1'] = true;
14                 }
15                 if ( board[j][i] != '.'){
16                     if(checkc[board[j][i] - '1']) return false;
17                     checkc[board[j][i] - '1'] = true;
18                 }
19             }
20         }
21         for(int a = 0; a < 3; a ++){
22             for(int b = 0; b < 3; b ++){
23                 checkr = new boolean[9];
24                 for(int i = a * 3; i < (a + 1) * 3; i ++){
25                     for(int j = b * 3; j < (b + 1) *3; j ++){
26                         if ( board[i][j]!='.' ){
27                             if(checkr[board[i][j] - '1']) return false;
28                             checkr[board[i][j] - '1'] = true;
29                         }
30                     }
31                 }
32             }
33         }
34         return true;
35     }
36 }

 第三遍:

 1 public class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         if(board == null || board.length != 9 || board[0].length != 9) return false;
 4         boolean[] value = new boolean[9];
 5         for(int i = 0; i < 9; i ++){
 6             value = new boolean[9];
 7             for(int j = 0; j < 9; j ++){
 8                 if(board[i][j] != '.' && value[board[i][j] - '1']) return false;
 9                 else if(board[i][j] != '.' && !value[board[i][j] - '1']) value[board[i][j] - '1'] = true;
10             }
11             value = new boolean[9];
12             for(int j = 0; j < 9; j ++){
13                 if(board[j][i] != '.' && value[board[j][i] - '1']) return false;
14                 else if(board[j][i] != '.' && !value[board[j][i] - '1']) value[board[j][i] - '1'] = true;
15             }
16         }
17         for(int k = 0; k < 3; k ++){
18             for(int h = 0; h < 3; h ++){
19                 value = new boolean[9];
20                 for(int i = 3*k; i < 3*k + 3; i ++){
21                     for(int j = 3*h; j < 3*h + 3; j ++){
22                         if(board[i][j] != '.' && value[board[i][j] - '1']) return false;
23                         else if(board[i][j] != '.' && !value[board[i][j] - '1']) value[board[i][j] - '1'] = true;
24                     }
25                 }
26             }
27         }
28         return true;
29     }
30 }

 

posted on 2013-10-13 07:40  Step-BY-Step  阅读(126)  评论(0编辑  收藏  举报

导航