您的位置:首页 > 职场人生

leetcode Valid Sudoku 2.12 难度系数2

2014-01-26 12:28 381 查看
Question

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



A partially filled sudoku which is valid.
import java.util.Hashtable;
public class Solution {
public boolean isValidSudoku(char[][] board) {

for (int i = 0; i < 9; i++) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c=='.') {

}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}

for (int i = 0; i < 9; ++i) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int j = 0; j < 9; ++j) {
char c = board[j][i];
if (c=='.') {

}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}

return isSmallValid(board, 0, 0) && isSmallValid(board, 3, 0)
&& isSmallValid(board, 6, 0) && isSmallValid(board, 0, 3)
&& isSmallValid(board, 3, 3) && isSmallValid(board, 6, 3)
&& isSmallValid(board, 0, 6) && isSmallValid(board, 3, 6)
&& isSmallValid(board, 6, 6);

}

private boolean isSmallValid(char[][] board, int x, int y) {
Hashtable<Character, Character> ht = new Hashtable<Character, Character>();
for (int i = x; i < 3 + x; ++i) {
for (int j = y; j < 3 + y; ++j) {
char c = board[i][j];
if (c=='.') {

}else{
if (ht.get(c)==null) {
ht.put(c, c);
}else {
return false;
}
}
}
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息