直接法和位运算发
1 public boolean isValidSudoku(char[][] board) { 2 int[][] line=new int[board.length][board.length]; 3 int[][] colum=new int[board.length][board.length]; 4 int[][] cell=new int[board.length][board.length]; 5 for (int i = 0; i < board.length; i++) { 6 for (int j = 0; j < board.length; j++) { 7 if (board[i][j]=='.') 8 continue; 9 int num=board[i][j]-'0'-1; 10 int k=i/3*3+j/3; 11 if (line[i][num]!=0||colum[num][j]!=0||cell[k][num]!=0) 12 return false; 13 line[i][num]=colum[num][j]=cell[k][num]=1; 14 } 15 } 16 return true; 17 } 18 public boolean isValidSudoku2(char[][] board) { 19 int[] line=new int[board.length]; 20 int[] colum=new int[board.length]; 21 int[] cell=new int[board.length]; 22 int numsBits=0; 23 for (int i = 0; i < board.length; i++) { 24 for (int j = 0; j < board.length; j++) { 25 if (board[i][j]=='.') 26 continue; 27 numsBits=1<<(board[i][j]-'0'); 28 int k=i/3*3+j/3; 29 if ((line[i]&numsBits)>0||(colum[j]&numsBits)>0||(cell[k]&numsBits)>0) 30 return false; 31 //此时的表示的每一行是否重复 32 line[i]|=numsBits; 33 colum[j]|=numsBits; 34 cell[k]|=numsBits; 35 36 } 37 } 38 return true; 39 }
标签:Java,colum,int,length,num,board,解决,数独,numsBits From: https://www.cnblogs.com/Mexcellent/p/17327750.html