LeetCode 36. Valid Sudoku 判断9*9的数独板是否有效

Determine if a 9x9 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 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.


A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

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:

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:

  • 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.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

方法一 借助set的特性分别判断行,列,子快是否有效

借助set无重复元素的特性来辅助判断。

class Solution {
public:
	static const set<char> num;

	bool isValidSudoku(vector<vector<char>>& board) {
		set<char> record;
		bool isValid = true;

		if (board.size() != 9 || board[0].size() != 9) {
			return false;
		}

		//先搜索所有行是否满足
		for (auto a : board) {
			record.clear();
			for (auto b : a) {
				if (b == '.') {
					continue;
				}
				if (num.find(b) == num.end()) {
					return false;
				}

				if (record.find(b) != record.end()) {
					isValid = false;
					return isValid;
				}
				else {
					record.insert(b);
				}
			}
		}
		//搜索所有列是否满足
		for (int i = 0; i < 9; ++i) {
			record.clear();
			for (int j = 0; j < 9; ++j) {
				if (board[j][i] == '.') {
					continue;
				}
				if (num.find(board[j][i]) == num.end()) {
					return false;
				}

				if (record.find(board[j][i]) != record.end()) {
					isValid = false;
					return isValid;
				}
				else {
					record.insert(board[j][i]);
				}
			}
		}

		//判断9个3*3的子快是否满足
		for (int m = 0; m <= 6; m += 3) {
			for (int n = 0; n <= 6; n += 3) {
				record.clear();
				for (int i = m; i < m + 3; ++i) {
					for (int j = n; j < n + 3; ++j) {
						if (board[i][j] == '.') {
							continue;
						}
						if (num.find(board[i][j]) == num.end()) {
							return false;
						}

						if (record.find(board[i][j]) != record.end()) {
							isValid = false;
							return isValid;
						}
						else {
							record.insert(board[i][j]);
						}
					}
				}
			}
		}
		return isValid;
	}
};

const set<char> Solution::num = { '1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };


方法二:建立三个辅助二维数组来判断行、列、子快是否有效

Three flags are used to check whether a number appear.

used1: check each row

used2: check each column

used3: check each sub-boxes

class Solution
{
public:
    bool isValidSudoku(vector<vector<char> > &board)
    {
        int used1[9][9] = {0}, used2[9][9] = {0}, used3[9][9] = {0};
        
        for(int i = 0; i < board.size(); ++ i)
            for(int j = 0; j < board[i].size(); ++ j)
                if(board[i][j] != '.')
                {
                    int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
                    if(used1[i][num] || used2[j][num] || used3[k][num])
                        return false;
                    used1[i][num] = used2[j][num] = used3[k][num] = 1;
                }
        
        return true;
    }
};

时间复杂度:O(n2)

猜你喜欢

转载自blog.csdn.net/qq_25800311/article/details/82836715