Shard my solution with 89% java


  • 0
    S

    //the whole idea is to reduce the number of comparision
    //After comparing the row and column, many of them are not necessary to be compared in the //little square
    public boolean isValidSudoku(char[][] board){
    for(int i = 0; i < 9; i++){
    if(!row(board,i)){
    return false;
    }
    }
    for(int i = 0; i < 9; i++){
    if(!line(board,i)){
    return false;
    }
    }
    for(int i = 0; i < 9; i++){
    if(!square(board,i)){
    return false;
    }
    }
    return true;
    }
    public boolean row(char[][] board, int row){
    int[] judge = new int[10];
    for(int i = 0; i < 9; i++){
    if(board[row][i] == '.'){
    continue;
    }
    int a = (int)board[row][i]-48 ;
    if(judge[a] != 0){
    return false;
    }
    judge[a] = 1;
    }
    return true;
    }
    public boolean line(char[][] board, int line){
    int[] judge = new int[10];
    for(int i = 0; i < 9; i++){
    if(board[i][line] == '.'){
    continue;
    }
    int a = (int)board[i][line]-48 ;
    if(judge[a] != 0){
    return false;
    }
    judge[a] = 1;
    }
    return true;
    }
    public boolean square(char[][] board, int num){
    int row = num/3;
    int line = num%3;
    int begin_row = 3row;
    int begin_line = 3
    line;
    for(int i = 0; i < 3; i++){
    int camparedNextRow = (i+1)%3;
    for(int m = 0; m < 3; m++){
    for(int j = (m+1)%3, k = 0; k < 2; j = (j+1)%3, k++){ //compare to next line
    if(board[begin_row + camparedNextRow][begin_line + j] == '.' || board[begin_row+i][begin_line+m] == '.'){
    continue;
    }
    if(board[begin_row + camparedNextRow][begin_line + j] == board[begin_row+i][begin_line+m]){
    return false;
    }
    }
    }

    	}
    	return true;
    }

Log in to reply
 

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