其他分享
首页 > 其他分享> > 36. 有效的数独 - LeetCode

36. 有效的数独 - LeetCode

作者:互联网

36. 有效的数独

题目链接

直接模拟

class Solution {
    public boolean isValidSudoku(char[][] board) {
        for(int i = 0; i < 9; i++){
            boolean[] marked = new boolean[9];
            for(int j = 0; j < 9; j++){
                char c = board[i][j];
                if(c == '.') continue;
                int index = c - '1';
                if(marked[index]) return false;
                marked[index] = true;
            }
        }
        for(int i = 0; i < 9; i++){
            boolean[] marked = new boolean[9];
            for(int j = 0; j < 9; j++){
                char c = board[j][i];
                if(c == '.') continue;
                int index = c - '1';
                if(marked[index]) return false;
                marked[index] = true;
            }
        }
        for(int i = 0; i < 9; i++){
            boolean[] marked = new boolean[9];
            for(int j = 0; j < 9; j++){
                char c = board[i/3*3+j/3][i%3*3+j%3];
                if(c == '.') continue;
                int index = c - '1';
                if(marked[index]) return false;
                marked[index] = true;
            }
        }
        return true;
    }
}

标签:index,int,36,marked,char,++,boolean,LeetCode,数独
来源: https://www.cnblogs.com/xiafrog/p/14415063.html