Seek Sudoku, only ask to make an answer.
Just beginning to understand test instructions wrong, think the answer is unique, so did not do it for a long time, found that the answer is not unique after using backtracking. (or a reference to others)
public class Solution {public void Solvesudoku (char[][] board) {hashset[] HashSet = new Hashset[27];for (int i = 0; I < 27; i++) Hashset[i] = new hashset<character> (); for (int i = 0, i < 9; i++) {for (int j = 0; J < 9; J + +) {Char Char = Board[i][j];if (Char! = '. ') {Hashset[i].add (char); hashset[9 + j].add (char); hashset[18 + (I/3) * 3 + j/3].add (char);}} int flag = 0;char[][][] num = null, while (flag = = 0) {flag = 1;num = new Char[9][9][9];for (int i = 0; i < 9; i++) {/ /I for the first hashsetfor (int j = 1; J <; J + +) {//k for 1-9char ch = (char) (j + ' 0 '); int[] test = new Int[2];if (!hashset[i ].contains (CH)) {Test[0] = 0;for (int k = 0; k < 9; k++) {Char ch = board[i][k];if (ch = = '. ') {if (!hashset[9 + k].contains (CH) &&!hashset[18 + (I/3) * 3 + k/3].contains (CH)) {addnum (num, I, k, ch); test[ 0]++;test[1] = k;}}} if (test[0] = = 1) {board[i][test[1]] = Ch;hashset[i].add (ch); flag = 0;hashset[9 + test[1]].add (CH); hashset[18 + (I/3) * 3 + tesT[1]/3].add (CH);}} for (int qq = 0, QQ < 9 && flag = = 1; qq++) {for (int j = 0; J < 9 && flag = = 1; j + +) {if (Getlen (nu M[QQ][J]) = = 1) {char ch = num[qq][j][0];board[qq][j] = Ch;flag = 0;hashset[qq].add (ch); hashset[9 + j].add (CH); hashset[18 + (QQ/3) * 3 + j/3].add (ch);}}} Solve (board); } public Boolean solve (char[][] board) {for (int i = 0; i < board.length; i++) {for (int j = 0; J &L T Board[0].length; J + +) {if (board[i][j] = = '. ') {for (char c = ' 1 '; c <= ' 9 '; C + +) {//trial. Try 1 through 9 for each cell if (isValid (board, I, J, c)) {Board[i][j] = C Put C for the This cell if (solve (board)) return true; If it ' s the solution return true else board[i][j] = '. '; Otherwise Go Back}} return false; }}} return true; } public boolean isValid (char[][] board, int i, Int. J, Char c) {for (int row = 0; row < 9; row++) I F (board[row][j] = = c) return false; for (int col = 0; col < 9; col++) if (board[i][col] = = c) return false; for (int row = (I/3) * 3, Row < (I/3) * 3 + 3; row++) for (int col = (J/3) * 3; col < (J/3) * 3 + 3 ; col++) if (board[row][col] = = c) return false; return true; public static int Getlen (char[] num) {int len = 0;for (int i = 0; i < 9; i++) {if (Num[i] < ' 1 ' | | num[i] > ' 9 ') {return len;} elselen++;} return Len;} public static void Addnum (char[][][] num, int num1, int. num2, Char ch) {for (int i = 0; i < 9; i++) {if (num[num1][num2 ][i] < ' 0 ' | | Num[num1][num2][i] > ' 9 ') {num[num1][num2][i] = Ch;break;}}}
Backtracking is still relatively simple, that is, in the implementation of the time, if you want to improve the speed and space for the operation, then it takes some thought to consider.
Attach code for reference
public class Solution {public void Solvesudoku (char[][] board) {if (board = = NULL | | board.length = = 0) Return Solve (board); } public Boolean solve (char[][] board) {for (int i = 0; i < board.length; i++) {for (int j = 0; J &L T Board[0].length; J + +) {if (board[i][j] = = '. ') {for (char c = ' 1 '; c <= ' 9 '; C + +) {//trial. Try 1 through 9 for each cell if (isValid (board, I, J, c)) {Board[i][j] = C Put C for the This cell if (solve (board)) return true; If it ' s the solution return true else board[i][j] = '. '; Otherwise go Back}} return false; }}} return true; } public boolean isValid (char[][] board, int i, Int J, char c) {//cHeck colum for (int row = 0; row < 9; row++) if (board[row][j] = = c) return false; Check row for (int col = 0; col < 9; col++) if (board[i][col] = = c) return false; Check 3 x 3 block for (int row = (I/3) * 3, Row < (I/3) * 3 + 3; row++) for (int col = (j /3) * 3; Col < (J/3) * 3 + 3; col++) if (board[row][col] = = c) return false; return true; }}
Leetcode Notoginseng Sudoku Solver java