10-line Java solution


  • 0
    A
    public boolean isValidSudoku(char[][] board) {
        int N = board.length;
        Set<Character>[] rows = new HashSet[N], cols = new HashSet[N], blocks = new HashSet[N];
        for(int i=0; i<N; i++){rows[i]=new HashSet<>(); cols[i]=new HashSet<>(); blocks[i]=new HashSet<>();}
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++){
                if(board[i][j]=='.') continue;
                int blockidx = i/3*3 + j/3;
                if(!rows[i].add(board[i][j]) || !cols[j].add(board[i][j]) || !blocks[blockidx].add(board[i][j])) return false;
            }
        return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.