1. 题目
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.
2. 思路
需要校验横、竖、方三个点,一共有27个9位向量。
将每一个点填入对一个的三个方形的标记数组中,如果已经填入过说明冲突了,就返回false。
3. 代码
class Solution {
public:
// 横竖方一共有27种需要验证1-9的独立性, 遍历一次填入这27个向量里, 如果已有则证明重复
bool isValidSudoku(vector<vector<char>>& board) {
int exist[27][9];
bzero(exist, sizeof(exist));
for (int i = 0; i < board.size(); i++) {
vector<char>& v = board[i];
for (int j = 0; j < v.size(); j++) {
char ch = v[j];
int chv = ch - '1';
if (ch == '.') continue;
int k1;
// i行
k1 = i;
//cout << "row i=" << i << " j=" << j << " k1=" << k1 << " exist=" << exist[k1][chv] << endl;
if (exist[k1][chv] != 0) return false;
exist[k1][chv] = 1;
// j列
k1 = 9 + j;
//cout << "col i=" << i << " j=" << j << " k1=" << k1 << " exist=" << exist[k1][chv] << endl;
if (exist[k1][chv] != 0) return false;
exist[k1][chv] = 1;
// 方形
k1 = 18 + (i / 3) * 3 + j / 3;
//cout << "squ i=" << i << " j=" << j << " k1=" << k1 << " exist=" << exist[k1][chv] << endl;
if (exist[k1][chv] != 0) return false;
exist[k1][chv] = 1;
}
}
return true;
}
};
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。