JAVA----------------Easy Version To UnderStand!!!!!!!!!!!!


  • 2
    H
    	public static boolean CheckOk(char[][] board, int i, int j) {
    	for (int k = 0; k < 9; k++) {
    		if (k == j)
    			continue;
    		else if (board[i][k] == board[i][j])
    			return false;
    	}
    	for (int k = 0; k < 9; k++) {
    		if (k == i)
    			continue;
    		else if (board[k][j] == board[i][j])
    			return false;
    	}
    
    	for (int p = i / 3 * 3; p < i / 3 * 3 + 3; p++)
    		for (int q = j / 3 * 3; q < j / 3 * 3 + 3; q++) {
    			if (p == i && q == j)
    				continue;
    			else if (board[p][q] == board[i][j])
    				return false;
    		}
    	return true;
    }
    
    public static boolean solveSudoku(char[][] board) {
    	//
    	for (int i = 0; i < board.length; i++)
    		for (int j = 0; j < board[0].length; j++) {
    			if (board[i][j] == '.') {
    				for (int k = 1; k <= 9; k++) {
    					board[i][j] = (char) (k + '0');//
    					if (CheckOk(board, i, j) && solveSudoku(board))
    						return true;
    					else
    						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.