Leetcode-Valid Sudoku

来源:互联网 发布:游戏秘籍输入器源码 编辑:程序博客网 时间:2024/06/07 09:50

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'.'.


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.


class Solution{public:bool isValidSudoku(vector<vector<char>> &board) {int cnt[10];for (int i = 0; i < 9; i++) {            //每一行满足的条件memset(cnt, 0, sizeof(cnt));for (int j = 0; j < 9; j++) {if (board[i][j] != '.') {cnt[board[i][j] - '0']++;if (cnt[board[i][j] - '0']>1)return false;}}}for (int j = 0; j < 9; j++) {            //每一列满足的条件memset(cnt, 0, sizeof(cnt));for (int i = 0; i < 9; i++) {if (board[i][j] != '.') {cnt[board[i][j] - '0']++;if (cnt[board[i][j] - '0']>1)return false;}}}for (int i = 0; i < 9; i += 3)          //每个宫满足的条件{for (int j = 0; j < 9; j += 3){memset(cnt, 0, sizeof(cnt));for (int a = i; a < i + 3; a++){for (int b = j; b < j + 3; b++){if (board[a][b] != '.'){cnt[board[a][b] - '0']++;if (cnt[board[a][b] - '0'] > 1)return false;}}}}}return true;}};