« Back
in Hash Table read.

Leetcode 36 Valid Sudoku.

Leetcode 36 Valid Sudoku

etermine 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 '.'.
The sudoku image

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.


Thought the system shows that this problem is a easy one, but if you want to write it correct is not so easy.


public class Solution {
    public boolean isValidSudoku(char[][] board) {
        for(int i=0;i < 9;i++){
            if(!ispartialvalid(board,i,0,i,8)){
                return false;
            }
            if(!ispartialvalid(board,0,i,8,i)){
                return false;
            }

        }
        for(int j=0;j < 3;j++){
            for(int i=0;i < 3;i++){
                if(!ispartialvalid(board,3*j,3*i,3*(j+1)-1,3*(i+1)-1)){
                    return false;
                }
            }

        }
        return true;
    }


    public boolean ispartialvalid(char[][] board,int x1,int y1,int x2, int y2){
        HashSet myhash=new HashSet();
        for(int i=x1;i < =x2;i++){
            for(int j=y1;j < =y2;j++){
                if(board[i][j]!='.'){
                    if(!myhash.add(board[i][j])){
                        return false;
                    }
                }

        }

    }
    return true;
}
}
comments powered by Disqus