首页 > 其他 > 详细

leetcode Valid Sudoku 2.12 难度系数2

时间:2014-01-26 15:59:49      阅读:400      评论:0      收藏:0      [点我收藏+]

Question

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

bubuko.com,布布扣

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;
		}
}


leetcode Valid Sudoku 2.12 难度系数2

原文:http://blog.csdn.net/yiding_he/article/details/18793859

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!