File tree Expand file tree Collapse file tree 4 files changed +123
-0
lines changed Expand file tree Collapse file tree 4 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ //Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2
+ //The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3
+ //A partially filled sudoku which is valid.
4
+
5
+ //Note:
6
+ //A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7
+
8
+ class ValidSudoku {
9
+ public boolean isValidSudoku (char [][] board ) {
10
+ for (int i = 0 ; i < board .length ; i ++){
11
+ HashSet <Character > rows = new HashSet <Character >();
12
+ HashSet <Character > columns = new HashSet <Character >();
13
+ HashSet <Character > box = new HashSet <Character >();
14
+ for (int j = 0 ; j < board [0 ].length ; j ++){
15
+ if (board [i ][j ] != '.' && !rows .add (board [i ][j ])) {
16
+ return false ;
17
+ }
18
+ if (board [j ][i ]!='.' && !columns .add (board [j ][i ])) {
19
+ return false ;
20
+ }
21
+ int rowIndex = (i / 3 ) * 3 ;
22
+ int columnIndex = (i % 3 ) * 3 ;
23
+ if (board [rowIndex + j / 3 ][columnIndex + j % 3 ] != '.' && !box .add (board [rowIndex + j / 3 ][columnIndex + j % 3 ])) {
24
+ return false ;
25
+ }
26
+ }
27
+ }
28
+ return true ;
29
+ }
30
+ }
31
+
Original file line number Diff line number Diff line change
1
+ //Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2
+ //The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3
+ //A partially filled sudoku which is valid.
4
+
5
+ //Note:
6
+ //A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7
+
8
+ class ValidSudoku {
9
+ public boolean isValidSudoku (char [][] board ) {
10
+ for (int i = 0 ; i < board .length ; i ++){
11
+ HashSet <Character > rows = new HashSet <Character >();
12
+ HashSet <Character > columns = new HashSet <Character >();
13
+ HashSet <Character > box = new HashSet <Character >();
14
+ for (int j = 0 ; j < board [0 ].length ; j ++){
15
+ if (board [i ][j ] != '.' && !rows .add (board [i ][j ])) {
16
+ return false ;
17
+ }
18
+ if (board [j ][i ]!='.' && !columns .add (board [j ][i ])) {
19
+ return false ;
20
+ }
21
+ int rowIndex = (i / 3 ) * 3 ;
22
+ int columnIndex = (i % 3 ) * 3 ;
23
+ if (board [rowIndex + j / 3 ][columnIndex + j % 3 ] != '.' && !box .add (board [rowIndex + j / 3 ][columnIndex + j % 3 ])) {
24
+ return false ;
25
+ }
26
+ }
27
+ }
28
+ return true ;
29
+ }
30
+ }
31
+
Original file line number Diff line number Diff line change
1
+ //Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2
+ //The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3
+ //A partially filled sudoku which is valid.
4
+
5
+ //Note:
6
+ //A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7
+
8
+ class ValidSudoku {
9
+ public boolean isValidSudoku (char [][] board ) {
10
+ for (int i = 0 ; i < board .length ; i ++){
11
+ HashSet <Character > rows = new HashSet <Character >();
12
+ HashSet <Character > columns = new HashSet <Character >();
13
+ HashSet <Character > box = new HashSet <Character >();
14
+ for (int j = 0 ; j < board [0 ].length ; j ++){
15
+ if (board [i ][j ] != '.' && !rows .add (board [i ][j ])) {
16
+ return false ;
17
+ }
18
+ if (board [j ][i ]!='.' && !columns .add (board [j ][i ])) {
19
+ return false ;
20
+ }
21
+ int rowIndex = (i / 3 ) * 3 ;
22
+ int columnIndex = (i % 3 ) * 3 ;
23
+ if (board [rowIndex + j / 3 ][columnIndex + j % 3 ] != '.' && !box .add (board [rowIndex + j / 3 ][columnIndex + j % 3 ])) {
24
+ return false ;
25
+ }
26
+ }
27
+ }
28
+ return true ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ //Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2
+ //The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3
+ //A partially filled sudoku which is valid.
4
+
5
+ //Note:
6
+ //A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7
+
8
+ class ValidSudoku {
9
+ public boolean isValidSudoku (char [][] board ) {
10
+ for (int i = 0 ; i < board .length ; i ++){
11
+ HashSet <Character > rows = new HashSet <Character >();
12
+ HashSet <Character > columns = new HashSet <Character >();
13
+ HashSet <Character > box = new HashSet <Character >();
14
+ for (int j = 0 ; j < board [0 ].length ; j ++){
15
+ if (board [i ][j ] != '.' && !rows .add (board [i ][j ])) {
16
+ return false ;
17
+ }
18
+ if (board [j ][i ]!='.' && !columns .add (board [j ][i ])) {
19
+ return false ;
20
+ }
21
+ int rowIndex = (i / 3 ) * 3 ;
22
+ int columnIndex = (i % 3 ) * 3 ;
23
+ if (board [rowIndex + j / 3 ][columnIndex + j % 3 ] != '.' && !box .add (board [rowIndex + j / 3 ][columnIndex + j % 3 ])) {
24
+ return false ;
25
+ }
26
+ }
27
+ }
28
+ return true ;
29
+ }
30
+ }
31
+
You can’t perform that action at this time.
0 commit comments