leetcode_36_有效的数独@@暴力

leetcode_36_有效的数独@@暴力_第1张图片
leetcode_36_有效的数独@@暴力_第2张图片

leetcode_36_有效的数独@@暴力_第3张图片

class Solution {
   	public boolean isValidSudoku(char[][] board) {

		for (int i = 0; i < 9; i++) {
			int[] rows = new int[10];
			for (int j = 0; j < 9; j++) {
				if (board[i][j] == '.')
					continue;
				int parseInt = board[i][j] - '0';
				if (rows[parseInt] > 0)
					return false;
				rows[parseInt]++;
			}
		}

		for (int i = 0; i < 9; i++) {
			int[] cols = new int[10];
			for (int j = 0; j < 9; j++) {
				if (board[j][i] == '.')
					continue;
				int parseInt = board[j][i] - '0';
				if (cols[parseInt] > 0)
					return false;
				cols[parseInt]++;
			}
		}
		
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				int[] boxes = new int[10];
				int row = i * 3;
				int col = j * 3;
				for (int k = row; k < row + 3; k++) {
					for (int m = col; m < col + 3; m++) {
						if (board[k][m] == '.')
							continue;
						int parseInt = board[k][m] - '0';
						if (boxes[parseInt] > 0)
							return false;
						boxes[parseInt]++;
					}
				}
			}
		}
		return true;
	}
}

你可能感兴趣的:(数据结构与算法)