码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Sudoku

时间:2014-06-27 09:38:48      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   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 ‘.‘.

bubuko.com,布布扣

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.

方法

只需要判断每一行,每一列以及每一个小矩形里面没有重复的元素即可。如果是空着的,默认为正确的。
    private boolean isValidRow(char[][] board) {
        int len = 9;
        boolean[] status = new boolean[len];
        for (int i = 0; i < len; i++) {
            for (int k = 0; k < len; k++) {
                status[k] = false;
            }
            
            for (int j = 0; j < len; j++) {
                if (board[i][j] != '.') {
                    if (status[board[i][j] - '1']) {
                        return false;
                    } else {
                        status[board[i][j] - '1'] = true;
                    }
                }
            }
         
        }
        return true; 
    }
    
    
    private boolean isValidColumn(char[][] board) {
        int len = 9;
        boolean[] status = new boolean[len];
        for (int i = 0; i < len; i++) {
            for (int k = 0; k < len; k++) {
                status[k] = false;
            }
            
            for (int j = 0; j < len; j++) {
                if (board[j][i] != '.') {
                    if (status[board[j][i] - '1']) {
                        return false;
                    } else {
                        status[board[j][i] - '1'] = true;
                    }
                }
            }
        }
        return true;
    }  
    
    private boolean isValidBoxes(char[][] board) {
        int len = 9;
        boolean[] status = new boolean[len];
        for (int i = 0; i < len; i = i + 3) {
            for (int j = 0; j < len; j = j + 3) {
                for (int k = 0; k < len; k++) {
                    status[k] = false;
                }
                
                for (int p = 0; p < 3; p++) {
                    for (int q = 0; q < 3; q++) {
                        if (board[i + p][j + q] != '.') {
                            if (status[board[i + p][j + q] - '1']) {
                                return false;
                            } else {
                               status[board[i + p][j + q] - '1'] = true; 
                            }
                        }
                    }
                }
                
                
            }
        }
        
        return true;
    }
    public boolean isValidSudoku(char[][] board) {
        return isValidRow(board) && isValidColumn(board) && isValidBoxes(board);
    }


Valid Sudoku,布布扣,bubuko.com

Valid Sudoku

标签:java   leetcode   sudoku   数组   

原文地址:http://blog.csdn.net/u010378705/article/details/34845489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!