Lettcode_36_Valid Sudoku

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42528601

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.


思路:

(1)题意为判断一个数独是否有效。

(2)题意不是让我们求解出整个数独(当然,如果真的求解还是很复杂的),而是判断数独中已有数据是否有效。

(3)本文的思路还是暴力破解,因为没有想到其它好的方法。对数独对应的9*9的二维数组进行遍历,对于任意一个不为'.'的数字,都需要对其所在的行、列、以及3*3的块区域进行判断,如果有重复出现的情况,那么数独就是无效的,具体见下方代码。

(4)希望本文对你有所帮助。


算法代码实现如下:

public boolean isValidSudoku(char[][] board) {
	for (int i = 0; i < board.length; i++) {
		for (int j = 0; j < board[i].length; j++) {
			char curr = board[i][j];
			if(curr=='.'){
				continue;
			}
			
			//行
			for (int k = j+1; k < board.length; k++) {
				if(curr==board[i][k]){
					return false;
				}
			}
			
			
			//列
			for (int k = i+1; k < board.length; k++) {
				if(curr==board[k][j]){
					return false;
				}
			}
			
			
			//3*3 方块
			if(i>=0&&i<3&&j>=0&&j<3){
				int count=0;
				for (int k1 = 0; k1 < 3; k1++) {
					for (int k2 = 0; k2 < 3; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			if(i>=0&&i<3&&j>=3&&j<6){
				int count=0;
				for (int k1 = 0; k1 < 3; k1++) {
					for (int k2 = 3; k2 < 6; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			if(i>=0&&i<3&&j>=6&&j<9){
				int count=0;
				for (int k1 = 0; k1 < 3; k1++) {
					for (int k2 = 6; k2 < 9; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			
			if(i>=3&&i<6&&j>=0&&j<3){
				int count=0;
				for (int k1 = 3; k1 < 6; k1++) {
					for (int k2 = 0; k2 < 3; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			if(i>=3&&i<6&&j>=3&&j<6){
				int count=0;
				for (int k1 = 3; k1 < 6; k1++) {
					for (int k2 = 3; k2 < 6; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			if(i>=3&&i<6&&j>=6&&j<9){
				int count=0;
				for (int k1 = 3; k1 < 6; k1++) {
					for (int k2 = 6; k2 < 9; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			
			if(i>=6&&i<9&&j>=0&&j<3){
				int count=0;
				for (int k1 = 6; k1 < 9; k1++) {
					for (int k2 = 0; k2 < 3; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			
			if(i>=6&&i<9&&j>=3&&j<6){
				int count=0;
				for (int k1 = 6; k1 < 9; k1++) {
					for (int k2 = 3; k2 < 6; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
			
			
			if(i>=6&&i<9&&j>=6&&j<9){
				int count=0;
				for (int k1 = 6; k1 < 9; k1++) {
					for (int k2 = 6; k2 < 9; k2++) {
						if(board[k1][k2]==curr){
							count++;
						}
						if(count>1) return false;
					}
				}
			}
		}
	}
	return true;
}


你可能感兴趣的:(java,LeetCode,二维数组,算法,数独)