【LeetCode】 61 有效的数独

题目:

image-20200720231038572

image-20200720231028789

image-20200720231010187

解题思路:

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

代码:

class Solution {
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] cols = new int[9][9];
        int[][] cells = new int[9][9];
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[i].length; j++){
                if(board[i][j]!="."){
                    int num = board[i][j]-"0";
                    if(rows[i][num-1]==1)return false;
                    rows[i][num-1]++;
                    if(cols[j][num-1]==1)return false;
                    cols[j][num-1]++;
                    int cell = (i/3) * 3 + (j/3);
                    if(cells[cell][num-1]==1)return false;
                    cells[cell][num-1]++;
                }
            }
        }
        return true;
    }

}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 61 有效的数独