-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
36 lines (33 loc) · 1.22 KB
/
Copy pathValidSudoku.java
File metadata and controls
36 lines (33 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.leetcode.string;
public class ValidSudoku {
/*
判断一个x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字1-9在每一行只能出现一次。
数字1-9在每一列只能出现一次。
数字1-9在每一个以粗实线分隔的3x3宫内只能出现一次。
数独部分空格内已填入了数字,空白格用'.'表示。
链接:https://leetcode-cn.com/problems/valid-sudoku
*/
public boolean isValidSudoku(char[][] board) {
int [][]rows = new int[9][10];//代表行
int [][]cols = new int[9][10];//代表列
int [][]boxes = new int[9][10];//代表3*3的格子
for(int r=0;r<board.length;r++){
for(int c=0;c<board[r].length;c++){
char ch = board[r][c];
if(ch=='.'){
continue;
}
int n = ch -'0';
rows[r][n] +=1;
cols[c][n] +=1;
int box= (r/3)*3+c/3;
boxes[box][n]+=1;
if(rows[r][n]>1||cols[c][n]>1||boxes[box][n]>1){
return false;
}
}
}
return true;
}
}