您的位置:首页 > 其它

[leetcode 36] valid sudoku

2015-09-27 19:52 387 查看
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 思路

开始就以为是完全填完,看最后能不能填出来。

结果题目意思是根据已有的数据,判断是否已经挂了。这也是看完别人的代码才知道的。。

3 代码:

// only need to examin the providing num is Valid
int len = board.length;
Boolean[][] row = new Boolean[len][len];
Boolean[][] column = new Boolean[len][len];
Boolean[][] sub = new Boolean[len][len];

for (int i = 0; i < sub.length; i++) {
Arrays.fill(row[i], false);
Arrays.fill(column[i], false);
Arrays.fill(sub[i], false);
}

for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(board[i][j] != '.'){
int num = board[i][j] - '1';
int k = i/3*3+j/3;
if(row[i][num] || column[j][num] || sub[k][num]){
return false;
}
row[i][num]=column[j][num]=sub[k][num] = true;
}
}
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: