More:【目錄】LeetCode Java實現html
https://leetcode.com/problems/valid-sudoku/java
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:git
1-9
without repetition.1-9
without repetition.3x3
sub-boxes of the grid must contain the digits 1-9
without repetition.
A partially filled sudoku which is valid.post
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.ui
Example 1:spa
Input: [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] Output: true
Example 2:3d
Input: [ ["8","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:code
1-9
and the character '.'
.9x9
.Mehtod 1: Use three hashSet (rowSet, colSet, boxSet) to store digits in each row/ column/ sub-box.htm
Details: blog
i: 1 ->9 j: i ->9
rowSet: a[i][j]
colSet: a[j][i]
boxSet:
For a given sub-box, we can traverse a 3*3 sub-box using just j: a[ j/3 ] [ j%3 ]
We has 9 sub-boxes, so we can use i to switch in different sub-boxes: a[i/3*3+j/3][i%3*3+j%3]
Mehtod 2: Use only one hashSet to store a[i][j] in three ways.
Encode a[i][j] as a[i][j]+"r"+i to represent a[i][j] in row i;
Encode a[i][j] as a[i][j]+"c"+j to represent a[i][j] in colum j;
Encode a[i][j] as a[i][j]+"box"+i/3+j/3 to represent a[i][j] in box-i/3-j/3;
//Method 1 public boolean isValidSudoku(char[][] board) { for(int i=0; i<9; i++){ HashSet<Character> rowSet = new HashSet<>(); HashSet<Character> colSet = new HashSet<>(); HashSet<Character> boxSet = new HashSet<>(); for(int j=0; j<9; j++){ if(board[i][j]!='.' && !rowSet.add(board[i][j])) return false; if(board[j][i]!='.' && !colSet.add(board[j][i])) return false; if(board[i/3*3+j/3][i%3*3+j%3]!='.' && !boxSet.add(board[i/3*3+j/3][i%3*3+j%3])) return false; } } return true; } //Method 2: easier to understand public boolean isValidSudoku(char[][] board) { HashSet<String> set = new HashSet<>(); for(int i=0; i<9; i++){ for(int j=0; j<9; j++){ if(board[i][j]!='.'){ String num = String.valueOf(board[i][j]); if(!set.add(num+"r"+i) || !set.add(num+"c"+j) || !set.add(num+"box"+i/3+j/3)) return false; } } } return true; }
Time complexity : O(n)
Space complexity : O(n)
1. Learn how to use i, j to represent elements in different rows and columns and sub-boxes.
2. For a given sub-box, we can traverse a 3*3 sub-box using just j: a[ j/3 ] [ j%3 ]
3. It's clever to store elements in a hashSet by convert elements to String.
More:【目錄】LeetCode Java實現