61 有效的数独
题目:
解题思路:
https://leetcode-cn.com/problems/valid-sudoku/solution/zi-yong-you-xiao-de-shu-du-by-agasar/
代码:
class Solution {
public boolean isValidSudoku(char[][] board) {
int[][] rows = new int[9][9];
int[][] cols = new int[9][9];
int[][] cells = new int[9][9];
for(int i=0; i<board.length; i++){
for(int j=0; j<board[i].length; j++){
if(board[i][j]!="."){
int num = board[i][j]-"0";
if(rows[i][num-1]==1)return false;
rows[i][num-1]++;
if(cols[j][num-1]==1)return false;
cols[j][num-1]++;
int cell = (i/3) * 3 + (j/3);
if(cells[cell][num-1]==1)return false;
cells[cell][num-1]++;
}
}
}
return true;
}
}