Java for LeetCode 036 Valid Sudoku

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 ‘.‘.

解题思路:

传说中的数独(九宫格)问题,老实遍历三个规则即可:

JAVA实现:

	static public boolean isValidSudoku(char[][] board) {
		for (int i = 0; i < board.length; i++) {
			HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
			for (int j = 0; j < board[0].length; j++) {
				if (board[i][j] != ‘.‘) {
					if (hashmap.containsKey(board[i][j]))
						return false;
					hashmap.put(board[i][j], 1);
				}
			}
		}
		for (int j = 0; j < board[0].length; j++) {
			HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
			for (int i = 0; i < board.length; i++) {
				if (board[i][j] != ‘.‘) {
					if (hashmap.containsKey(board[i][j]))
						return false;
					hashmap.put(board[i][j], 1);
				}
			}
		}
	    for (int i = 0; i < board.length; i += 3){
	       for (int j = 0; j < board[0].length; j += 3){
	           HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
				for (int k = 0; k < 9; k++) {
					if (board[i + k / 3][j + k % 3] != ‘.‘) {
						if (hashmap.containsKey(board[i + k / 3][j + k % 3]))
							return false;
						hashmap.put(board[i + k / 3][j + k % 3], 1);
					}
				}
			}
		}
		return true;
	}
时间: 2024-10-13 12:09:08

Java for LeetCode 036 Valid Sudoku的相关文章

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

LeetCode 036 Valid Sudoku

题目要求:Valid Sudoku 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

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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 (

【Leetcode】036. Valid Sudoku

题目: 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 (partia

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku 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 sud

【LeetCode】Valid Sudoku

Valid Sudoku 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 boar

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku 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 boa

LeetCode(38)-Valid Sudoku

题目: 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 '.'. ![这里写图片描述](http://img.blog.csdn.net/20160409183641502) A partially filled s

leetCode 36. Valid Sudoku(数独) 哈希

36. Valid Sudoku(合法数独) 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 S