您的位置:首页 > 其它

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



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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: