LeetCode:有效的数独

题目:

我的答案:

本来没思路,然后看到用数组来存储数字是否存在,就写了,代码如下:

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] row = new boolean[9][9];
        boolean[][] col = new boolean[9][9];
        boolean[][] grid = new boolean[9][9];

        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                if(board[i][j]!='.'){
                    int digit = board[i][j]-'0';//转为数字
                    if(row[i][digit-1]||col[j][digit-1]||grid[(i/3)*3+(j/3)][digit-1]){
                        return false;
                    }else{
                        row[i][digit-1] = true;
                        col[j][digit-1]= true;
                        grid[(i/3)*3+(j/3)][digit-1] = true;
                    }
                }
                
            }
        }
        return true;
    }
}

题解:

官方题解(思路差不多:

https://leetcode-cn.com/problems/valid-sudoku/solution/you-xiao-de-shu-du-by-leetcode/

总结:

没啥总结

发布了75 篇原创文章 · 获赞 2 · 访问量 8007

猜你喜欢

转载自blog.csdn.net/wyplj2015/article/details/104797855