leetcode- Valid Sudoku

https://leetcode.com/problems/valid-sudoku/description/
In a 9 * 9 matrix, check if each numbers (0-9) doesn’t repeat in each row, col and 3 * 3 matrix.

  • use a HashSet or array to check if the number is used.
  • check 3*3 squart –> board [ i + k/3 ][ j + k%3 ] (i, j are 9*9 board index, k is the check board (0-9) index)
  • if(digit > 9 || digit < 1 || check[digit -1]), check digit is valid first then check if the number is used

 

class Solution {
    public boolean isValidSudoku(char[][] board) {
               if(board == null || board.length != 9 || board[0].length != 9){
                System.out.print("1");
                return false;
            }

            boolean[] check = new boolean[9];

            for(int i=0 ; i &lt; 9 ; i++){
                Arrays.fill(check, false);

                for(int j=0; j 9 || digit &lt; 1 || check[digit -1]){
                            return false;
                        }
                        check[digit-1] = true;
                    }
                }
            }

            for(int i=0 ; i &lt; 9 ; i++){
                Arrays.fill(check, false);

                for(int j=0; j 9 || digit &lt; 1 || check[digit -1]){
                            return false;
                        }
                        check[digit-1] = true;
                    }
                }
            }

            for(int i=0 ; i &lt; 9 ; i+=3){
                for(int j=0; j&lt; 9 ; j+=3){
                    Arrays.fill(check, false);

                    for(int k=0 ; k 9 || digit <span id="mce_SELREST_end" style="overflow:hidden;line-height:0;"></span>&lt; 1 || check[digit -1]){
                                return false;
                            }
                            check[digit-1] = true;
                        }
                    }
                }
            }

            return true;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s