标签:style blog http io color ar sp for div
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘
.
这里我用定义来做的,代码有点长
先看所有行是否有效
所有列是否有效
所有小9宫格是否有效
1 public class Solution { 2 public boolean isValidSudoku(char[][] board) { 3 boolean isValid = true; 4 //所有行有效 5 for(int i = 0; i < 9;i++){ 6 if(!isValid(board[i])){ 7 isValid = false; 8 break; 9 } 10 } 11 //所有列 12 for(int i = 0; i < 9 && isValid; i++){ 13 char array[] = new char[9]; 14 for(int j = 0; j < 9; j++){ 15 array[j] = board[j][i]; 16 } 17 if(!isValid(array)){ 18 isValid = false; 19 break; 20 } 21 } 22 //所有小9宫格 23 for(int i = 0; i < 9 && isValid;i = i + 3){ 24 for(int j = 0; j < 9;j = j + 3){ 25 char array[] = new char[9]; 26 int x = 0; 27 for(int k = i; k < i + 3; k++){ 28 for(int p = j; p < j + 3;p++){ 29 array[x++] = board[k][p]; 30 } 31 } 32 if(!isValid(array)){ 33 isValid = false; 34 break; 35 } 36 } 37 } 38 if(isValid) 39 return true; 40 return false; 41 } 42 43 /** 44 * 判断数组是否有效 45 * @param array 46 * @return 47 */ 48 public boolean isValid(char []array){ 49 boolean isValid = true; 50 boolean one2Nine[] = new boolean[10]; 51 //System.out.println(one2Nine[0]); 52 for(int i = 0; i < 9;i++){ 53 if(‘.‘ != array[i]){ 54 if(!one2Nine[array[i] - ‘0‘]){ 55 one2Nine[array[i] - ‘0‘] = true; 56 }else{ 57 isValid = false; 58 break; 59 } 60 } 61 } 62 if(isValid) 63 return true; 64 else 65 return false; 66 }
标签:style blog http io color ar sp for div
原文地址:http://www.cnblogs.com/luckygxf/p/4095352.html