36. Valid Sudoku

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 or '.'.
class Solution {
public:
    //核心:输入的每一行、每一列、每一个3*3 box只出现一次1-9中的某个数字.
    //利用某个set 来分别区分也可以。本质上是搞个标志。
    bool isValidSudoku(vector<vector<char>>& board) {
        //判定每一行数字出现情况.第二个9表示数字0-8
        bool used1[9][9] = {false};
        //判定每一列数字出现情况,第二个9表示数0-8
        bool used2[9][9] = {false};
        //判定每个3*3 box数字出现情况,第一个9表示0-8个box
        bool used3[9][9] = {false}; 
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[i].size();j++){
                if(board[i][j] == '.')
                    continue;
                int num = board[i][j]-'0'-1;
                if(num < 0 || num > 8) return false;
                //第i行或者第j列出现过.used数组只是标志数组
                if(used1[i][num] || used2[j][num])
                    return false;
                used1[i][num] = true;
                used2[j][num] = true;
                //目前的board[i][j]在第几个box(00 01 02、10 11 12、20 21 22)
                int box = (i/3)*3+j/3;
                if(used3[box][num])
                    return false;
                used3[box][num] = true;
            }
        }
        return true;
    }
};

 

//法二也很巧妙:利用三类标志

class Solution {
public:
    //利用set来区分。
    //假设第1行出现3即表示为 (1)3
    //假设第1列出现3即表示为 3(1)
    //9个box中,i/3 与 j/3组合共有9个分别
    //00 01 02
    //10 11 12
    //20 21 22
    //那么第一个box出现了3表示为 0(3)0.
    bool isValidSudoku(vector<vector<char>>& board) {
        set<string> SudoRow;
        set<string> SudoCol;
        set<string> SudoBox;
        //boad[i][j]是char
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[i].size();j++){
                if(board[i][j] == '.') continue;
                if(board[i][j]-'0'-1 < 0 || board[i][j]-'0'-1 > 8)
                    return false;
                string row = "("+to_string(i)+")"+board[i][j];
                string col = to_string(j)+"("+board[i][j]+")";
                string box = to_string(i/3)+"("+board[i][j]+")"+to_string(j/3);
                if(SudoRow.find(row) != SudoRow.end() || SudoCol.find(col) != SudoCol.end() || SudoBox.find(box) != SudoBox.end()){
                    return false;
                }
                SudoRow.insert(row);
                SudoCol.insert(col);
                SudoBox.insert(box);
            }
        }
        return true;
    }
};

 

posted on 2020-10-18 16:12  wsw_seu  阅读(94)  评论(0编辑  收藏  举报

导航