【Leetcode Algorithm】Valid Sudoku
2015-07-03 10:20
495 查看
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.
public class Solution {
public boolean isValidSudoku(char[][] board) {
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
//判断每行是否有相同元素出现
for(int i=0;i<9;i++){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int j=0;j<9;j++){
if('1'<=board[i][j]&&board[i][j]<='9'){
int value = map.get(board[i][j])+1;
map.put(board[i][j], value);
if(map.get(board[i][j])>1){
return false;
}
}
}
}
//判断每列是否有相同元素出现
for(int i=0;i<9;i++){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int j=0;j<9;j++){
if('1'<=board[j][i]&&board[j][i]<='9'){
int value = map.get(board[j][i])+1;
map.put(board[j][i], value);
if(map.get(board[j][i])>1){
return false;
}
}
}
}
//判断每一个子格里(sub-boxes of the grid)是否有相同元素
for(int i=0;i<9;i=i+3){
for(int j=0;j<9;j=j+3){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int k=0;k<3;k++){
for(int x=0;x<3;x++){
if('1'<=board[i+k][j+x]&&board[i+k][j+x]<='9'){
int value = map.get(board[i+k][j+x])+1;
map.put(board[i+k][j+x], value);
if(map.get(board[i+k][j+x])>1){
return false;
}
}
}
}
}
}
return true;
}
}
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.
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.
public class Solution {
public boolean isValidSudoku(char[][] board) {
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
//判断每行是否有相同元素出现
for(int i=0;i<9;i++){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int j=0;j<9;j++){
if('1'<=board[i][j]&&board[i][j]<='9'){
int value = map.get(board[i][j])+1;
map.put(board[i][j], value);
if(map.get(board[i][j])>1){
return false;
}
}
}
}
//判断每列是否有相同元素出现
for(int i=0;i<9;i++){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int j=0;j<9;j++){
if('1'<=board[j][i]&&board[j][i]<='9'){
int value = map.get(board[j][i])+1;
map.put(board[j][i], value);
if(map.get(board[j][i])>1){
return false;
}
}
}
}
//判断每一个子格里(sub-boxes of the grid)是否有相同元素
for(int i=0;i<9;i=i+3){
for(int j=0;j<9;j=j+3){
for(int m=0;m<9;m++){
map.put((char)('1'+m),0);
}
for(int k=0;k<3;k++){
for(int x=0;x<3;x++){
if('1'<=board[i+k][j+x]&&board[i+k][j+x]<='9'){
int value = map.get(board[i+k][j+x])+1;
map.put(board[i+k][j+x], value);
if(map.get(board[i+k][j+x])>1){
return false;
}
}
}
}
}
}
return true;
}
}
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.
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- [转]可视化的数据结构和算法
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解