Java AC
public class Solution { public boolean isValidSudoku(char[][] board) { int n = board.length; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == '.'){ continue; } for(int k = 0; k < n; k++){ if (k == j) { continue; } if(board[i][k] == board[i][j]){ // System.out.println("1 "+i+" "+j ); return false; } } for(int k = 0; k < n; k++){ if (k == i) { continue; } if(board[k][j] == board[i][j]){ // System.out.println("2 "+i+" "+j ); return false; } } int rowStart = i / 3 * 3; int rowEnd = rowStart+3; int colStart = j / 3 * 3; int colEnd = colStart+3; for(int k = rowStart; k < rowEnd; k++){ for(int l = colStart; l < colEnd; l++){ if(k == i && l == j){ continue; } if(board[k][l] == board[i][j]){ // System.out.println("3 "+i+" "+j ); return false; } } } } } return true; } }2、思路:dfs,最开始的时候考虑边界问题。后来看到某个大牛写的代码,思路还是很不错的,可以借鉴。其实就是将所有需要填数字的坐标拿出来,从1到9依次判断是否可以填写,如果不对就回溯,否则继续。最后返回true,即结束。
Java AC
public class Solution { public int n, size; public ArrayList<Integer> list; public void solveSudoku(char[][] board) { n = board.length; list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i][j] == '.') { list.add(i * n + j); } } } size = list.size(); dfs(board, 0); } public boolean dfs(char[][] board, int cur) { if (cur == size) { return true; } int index = list.get(cur); int row = index / n; int col = index % n; for (int i = 1; i <= 9; i++) { if (isValidSudoku(board, row, col, (char)(i+'0'))) { board[row][col] = (char)(i+'0'); if (dfs(board, cur+1)) { return true; } board[row][col] = '.'; } } return false; } public boolean isValidSudoku(char[][] board, int i, int j, Character value) { for (int k = 0; k < n; k++) { if (k == j) { continue; } if (board[i][k] == value) { // System.out.println("1 "+i+" "+j ); return false; } } for (int k = 0; k < n; k++) { if (k == i) { continue; } if (board[k][j] == value) { // System.out.println("2 "+i+" "+j ); return false; } } int rowStart = i / 3 * 3; int rowEnd = rowStart + 3; int colStart = j / 3 * 3; int colEnd = colStart + 3; for (int k = rowStart; k < rowEnd; k++) { for (int l = colStart; l < colEnd; l++) { if (k == i && l == j) { continue; } if (board[k][l] == value) { // System.out.println("3 "+i+" "+j ); return false; } } } return true; } }