[leetcode 36] valid sudoku

1 题目

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.

2 思路

开始就以为是完全填完,看最后能不能填出来。

结果题目意思是根据已有的数据,判断是否已经挂了。这也是看完别人的代码才知道的。。

3 代码:

        // only need to examin the providing num is Valid
        int len = board.length;
        Boolean[][] row = new Boolean[len][len];
        Boolean[][] column = new Boolean[len][len];
        Boolean[][] sub = new Boolean[len][len];
        
        for (int i = 0; i < sub.length; i++) {
            Arrays.fill(row[i], false);
            Arrays.fill(column[i], false);
            Arrays.fill(sub[i], false);
        }

        
        for(int i=0;i<len;i++){
            for(int j=0;j<len;j++){
                if(board[i][j] != '.'){
                    int num = board[i][j] - '1';
                    int k = i/3*3+j/3;
                    if(row[i][num] || column[j][num] || sub[k][num]){
                        return false;
                    }
                    row[i][num]=column[j][num]=sub[k][num] = true;
                }
            }
        }
        return true;
    }
原文地址:https://www.cnblogs.com/lingtingvfengsheng/p/4842639.html