问题: 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 '.'. LeetCode-36.有效数独 - 图1

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.

There are just 3 rules to Sudoku

1) Each row must have the numbers 1-9 occuring just once.
LeetCode-36.有效数独 - 图2

2) Each column must have the numbers 1-9 occuring just once.
LeetCode-36.有效数独 - 图3

3) And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
LeetCode-36.有效数独 - 图4

每一行必须是数字1~9且不重复
每一列必须是数字1~9且不重复
每一个小九宫格(互不交叉,总共九个小九宫格)必须是数字1~9且不重复

思路分析

依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.
难点在于表示第i个九宫格每个格点的坐标。

观察行号规律:
第0个九宫格:000111222; 第1个九宫格:000111222; 第2个九宫格:000111222;
第3个九宫格:333444555; 第4个九宫格:333444555; 第5个九宫格:333444555;
第6个九宫格:666777888; 第7个九宫格:666777888; 第8个九宫格:666777888;
可见对于每三个九宫格行号增3;对于单个九宫格,每三个格点行号增1。
因此第i个九宫格的第j个格点的行号可表示为i/33+j/3(每个小九宫格j都是从0~9递增)

观察列号规律:
第0个九宫格:012012012; 第1个九宫格:345345345; 第2个九宫格:678678678;
第3个九宫格:012012012; 第4个九宫格:345345345; 第5个九宫格:678678678;
第6个九宫格:012012012; 第7个九宫格:345345345; 第8个九宫格:678678678;
可见对于下个九宫格列号增3,循环周期为3;对于单个九宫格,每个格点行号增1,周期也为3。
周期的数学表示就是取模运算mod。
因此第i个九宫格的第j个格点的列号可表示为i%33+j%3(每个小九宫格j都是从0~9递增)

HashSet实现 (Java)

  1. public boolean isValidSudoku(char[][] board) {
  2. for (int i = 0; i < 9; i++) {
  3. HashSet<Character> row = new HashSet<>();
  4. HashSet<Character> column = new HashSet<>();
  5. HashSet<Character> cube = new HashSet<>();
  6. for (int j = 0; j < 9; j++) {
  7. // 检查第i行,在横坐标位置
  8. if (board[i][j] != '.' && !row.add(board[i][j]))
  9. return false;
  10. // 检查第i列,在纵坐标位置
  11. if (board[j][i] != '.' && !column.add(board[j][i]))
  12. return false;
  13. // 行号+偏移量
  14. int RowIndex = 3 * (i / 3) + j / 3;
  15. // 列号+偏移量
  16. int ColIndex = 3 * (i % 3) + j % 3;
  17. //每个小九宫格,总共9个
  18. if (board[RowIndex][ColIndex] != '.'
  19. && !cube.add(board[RowIndex][ColIndex]))
  20. return false;
  21. }
  22. }
  23. return true;
  24. }

HashSet实现 (C++)

  1. bool isValidSudoku_hashset(vector<vector<char>>& board) {
  2. unordered_set<char> row;
  3. unordered_set<char> column;
  4. unordered_set<char> cube;
  5. for (int i = 0; i < 9; i++) {
  6. for (int j = 0; j < 9; j++) {
  7. if (board[i][j] != '.' && row.find(board[i][j])!=row.end()) {
  8. return false;
  9. }
  10. else {
  11. row.insert(board[i][j]);
  12. }
  13. if (board[j][i] != '.' && column.find(board[j][i])!=column.end()) {
  14. return false;
  15. }
  16. else {
  17. column.insert(board[j][i]);
  18. }
  19. int row_index = 3 * (i / 3) + j / 3;
  20. int column_index = 3 * (i % 3) + j % 3;
  21. if (board[row_index][column_index] != '.' && cube.find(board[row_index][column_index])!=cube.end()) {
  22. cout << row_index << "," << column_index << endl;
  23. return false;
  24. }
  25. else {
  26. cube.insert(board[row_index][column_index]);
  27. }
  28. }
  29. }
  30. return true;
  31. }

位图法实现 (Java)

// 使用位图法,出现置为1
public boolean isValidSudoku(char[][] board) {
    for (int i = 0; i < 9; i++) {
        int[] bit_map_row = new int[9];
        int[] bit_map_col = new int[9];
        int[] bit_map_cube = new int[9];
        // 注意值减去1,下标从0开始
        for (int j = 0; j < 9; j++) {
            if (board[i][j] != '.')
                if (bit_map_row[board[i][j] - '1'] == 1)
                    return false;
            else
                bit_map_row[board[i][j] - '1'] = 1;
            if (board[j][i] != '.')
                if (bit_map_col[board[j][i] - '1'] == 1)
                    return false;
            else
                bit_map_col[board[j][i] - '1'] = 1;
            int RowIndex = 3 * (i / 3) + j / 3;
            int ColIndex = 3 * (i % 3) + j % 3;
            int val = board[RowIndex][ColIndex];
            if (val != '.')
                if (bit_map_cube[val - '1'] == 1)
                    return false;
            else
                bit_map_cube[val - '1'] = 1;
        }
    }
    return true;
}