Why my answer is wrong?


  • 0
    A

    public class Solution {
    public void solveSudoku(char[][] board) {
    int rows =9 ,cols = 9;
    sudoku(0,-1,board,rows,cols) ;
    }

    public boolean sudoku(int x,int y,char[][] board ,int rows ,int cols){
    	if(x == rows -1 && y == cols-1 ){
    		return true;
    	}
    	else{
    		if(y == cols -1){
    			x = x+1;
    			y =0;
    		}else
    			y ++;
    	}
    	
    	if(board[x][y] !='.'){
    		return sudoku(x,y,board,rows,cols);
    			
    	}else{
    		char[] v = validate(x,y,board,rows,cols);
    		for(int i=0; i< v.length  ;i++){
    			if(v[i]=='0')
    				continue;
    			
    			board[x][y] = v[i];
    			if(sudoku(x,y,board,rows,cols) == true)
    					return true;
    		}
    			board[x][y] ='.';
    			return false;
    	}
    	
    }
    
    public char[] validate(int x, int y, char[][] board,int rows, int cols){
    	char[] v = {'1','2','3','4','5','6','7','8','9',};
    	for(int i=0; i< rows;i++){
    		if(board[i][y] != '.'){
    			v[board[i][y]-'0'-1] ='0';
    		}
    		if(board[x][i] != '.'){
    			v[board[x][i]-'0'-1] ='0';
    		}
    	}
    	return v;
    }
    

    }

    input: ["..9748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."]

    my output: ["139748652","712456839","325169478","987631245","864312597","498527361","576893124","253984716","641275983"]

    Expected: ["519748632","783652419","426139875","357986241","264317598","198524367","975863124","832491756","641275983"]

    i find my answer is correct;


  • 0
    Z

    May be there is a problem your answer.
    Your first 3*3 zone is
    1 3 9
    7 1 2
    3 2 3
    .
    1, 2 and 3 appear twice.


Log in to reply
 

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