1. 程式人生 > 其它 >LeetCode 36. Valid Sudoku (Medium)

LeetCode 36. Valid Sudoku (Medium)

題目

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

1.Each row must contain the digits 1-9 without repetition.
2.Each column must contain the digits 1-9 without repetition.
3.Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9

without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.


Example 1:

Input: board = 
[["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:

Input: board = 
[["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.


Constraints:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'.

思路

方法1 (Java)

設定一個Set集合,遍歷數獨表二維陣列,將當前遍歷到的位置的元素以3個字串形式記錄下它所在的行,列和九宮格資訊,並加入Set集合。若Set集合中存在某一元素的任意行/列/九宮格資訊,則認為該表不符合規則。
設當前元素值為val,位置為(i, j),位於第i/3*3+j/3個九宮格(從0開始),則該元素的資訊可以設定為:
行資訊:"val in row i"
列資訊:"val in col j"
九宮格資訊:"val in box i/3*3+j/3"

class Solution {
    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] == '.') continue;
                String row = board[i][j] + " in row " + i;
                String col = board[i][j] + " in col " + j;
                String box = board[i][j] + " in box " + (i/3*3+j/3);
                if(set.contains(row) || set.contains(col) || set.contains(box)) return false;
                set.add(row);
                set.add(col);
                set.add(box);
            }
        }
        return true;
    }
}

方法2(Java)

0-8,對每一個i,設定3個boolean陣列分別檢測第i行、第i列和第i個九宮格,陣列的index代表被檢測的值。
對於每個i的值,j0-8意味著檢測:
i行, 第j個數是否已位於行i中;
i列,第j個數是否已位於列i中;
i個九宮格中,該九宮格從左上角到右下角的第j數是否已位於該九宮格中。

設定3個數組,
boolean[] checkRow 檢查位置(i, j)的值是否已存在第i行
boolean[] checkCol 檢查位置(j, i)的值是否已存在第i列
boolean[] checkBox 檢查位置(i/3*3+j/3, j%3*3+j%3)的值是否已存在第i個九宮格中

例如當i為4,j從0-8,對應行、列、九宮格的遍歷過程

  0 1 2   3 4 5   6 7 8 
0 o o o | o 1 o | o o o
1 o o o | o 2 o | o o o 
2 o o o | o 3 o | o o o
------------------------
3 o o o | o 4 o | o o o              
4 1 2 3 | 4 5 6 | 7 8 9
5 o o o | o 6 o | o o o
------------------------
6 o o o | o 7 o | o o o
7 o o o | o 8 o | o o o
8 o o o | o 9 o | o o o

第4個九宮格中
1 2 3 
4 5 6
7 8 9
class Solution {
    public boolean isValidSudoku(char[][] board) {
        for(int i = 0; i < 9; i++){
            boolean[] checkRow = new boolean[9];
            boolean[] checkCol = new boolean[9];
            boolean[] checkBox = new boolean[9];
            for(int j = 0; j < 9; j++){
                if(board[i][j] == '.') {}
                else if(checkRow[board[i][j]-'1']) return false;
                else checkRow[board[i][j]-'1'] = true;
                
                if(board[j][i] == '.'){}
                else if(checkCol[board[j][i]-'1']) return false;
                else checkCol[board[j][i]-'1'] = true;
                
                int m = i/3*3+j/3, n = i%3*3+j%3;
                if(board[m][n] == '.'){}
                else if(checkBox[board[m][n]-'1']) return false;
                else checkBox[board[m][n]-'1'] = true;
            }
        }
        return true;
    }
}