judge whether the rule of Sudoku is met. Sudoku rule: Each row can not have duplicate numbers, each of the 3x3 squares must not have duplicate numbers, but the problem may be empty is '. '.
(to develop good programming habits ah, or a little low-level error is not easy to find, wasting life!) )
public class Solution {public boolean Isvalidsudoku (char[][] board) {StringBuilder str = new StringBuilder (""); rowfor (int i=0; i<9; i++) {for (int j=0; j<9; J + +) {if (board[i][j]! = '. ') {str.append (board[i][j]);}} if (Isonce (str.tostring ())) {str.delete (0, Str.length ());} else {return false;}} colfor (int i=0; i<9; i++) {for (int j=0; j<9; J + +) {if (board[j][i]! = '. ') {str.append (board[j][i]);}} if (Isonce (str.tostring ())) {str.delete (0, Str.length ());} else {return false;}} 3x3for (int cubex=0; cubex<=6; cubex+=3) {for (int cubey=0; cubey<=6; cubey+=3) {if (!iscubeonce, board, Cubey)) {return false;}}} return true; }public Static Boolean iscubeonce (char[][] charr, int i, int j) {StringBuilder str = new StringBuilder (""); for (int ii=i; ii<i+3; ii++) {for (int jj=j; jj<j+3; jj++) {if (CHARR[II][JJ]! = '. ') Str.append (CHARR[II][JJ]);}} if (Isonce (str.tostring ())) {return true;} else {return false;}} public static Boolean isonce (String s) {Java.util.set<character> set = new Java.util.hashset<> (), for (int i=0; i<s.length (); i++) {Set.add (S.charat (i));} Return Set.size () ==s.length ();}}
Leetcode-valid Sudoku