leetcode--Valid Sudoku
2017-08-08 09:16
323 查看
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://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
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.
[java] view
plain copy
public class Solution {
public boolean isValidSudoku(char[][] board) {
int rowValid[] = new int[10];//用于判断某一行是否合法,对于行来说这个数组可以重复使用
int columnValid[][] = new int[9][10];//用于判断某一列是否合法
int subBoardValid[][] = new int[9][10];//用于判断某一个九宫格是否合法
for(int i = 0; i < 9; i++){
Arrays.fill(rowValid, 0);
for(int j = 0; j < 9; j++){
if(board[i][j] != '.'){
if(!checkValid(rowValid, board[i][j]-'0') ||
!checkValid(columnValid[j], board[i][j]-'0') ||
!checkValid(subBoardValid[i/3*3+j/3], board[i][j]-'0'))
return false;
}
}
}
return true;
}
boolean checkValid(int[] nums,int target){
if(nums[target] == 1)return false;
nums[target] = 1;
return true;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45679623
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
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.
[java] view
plain copy
public class Solution {
public boolean isValidSudoku(char[][] board) {
int rowValid[] = new int[10];//用于判断某一行是否合法,对于行来说这个数组可以重复使用
int columnValid[][] = new int[9][10];//用于判断某一列是否合法
int subBoardValid[][] = new int[9][10];//用于判断某一个九宫格是否合法
for(int i = 0; i < 9; i++){
Arrays.fill(rowValid, 0);
for(int j = 0; j < 9; j++){
if(board[i][j] != '.'){
if(!checkValid(rowValid, board[i][j]-'0') ||
!checkValid(columnValid[j], board[i][j]-'0') ||
!checkValid(subBoardValid[i/3*3+j/3], board[i][j]-'0'))
return false;
}
}
}
return true;
}
boolean checkValid(int[] nums,int target){
if(nums[target] == 1)return false;
nums[target] = 1;
return true;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45679623
相关文章推荐
- [leetcode]Valid Sudoku
- LeetCode: Valid Sudoku 解题报告
- leetcode-36-valid sudoku
- 【Leetcode】Valid Sudoku
- leetcode Valid Sudoku 2.12 难度系数2
- LeetCode Valid Sudoku 有效数独
- [LeetCode]Valid Sudoku
- LeetCode:Valid Sudoku
- LeetCode: 36. Valid Sudoku
- Leetcode 细节实现 Valid Sudoku
- [C++]LeetCode: 28 Valid Sudoku
- 【Leetcode】36. Valid Sudoku
- LeetCode Online Judge 题目C# 练习 - Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- LeetCode(38)-Valid Sudoku
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
- LeetCode | Valid Sudoku
- [leetcode]Valid Sudoku
- LeetCode-36-Valid Sudoku(模拟/遍历)-Easy
- LeetCode 117 Valid Sudoku