leetcode Valid Sudoku 2.12 难度系数2

2014-11-24 08:30:47 · 作者: · 浏览: 0

Question

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

\

A partially filled sudoku which is valid.< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">import java.util.Hashtable; public class Solution { public boolean isValidSudoku(char[][] board) { for (int i = 0; i < 9; i++) { Hashtable ht = new Hashtable (); 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 ht = new Hashtable (); 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 ht = new Hashtable (); 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; } }