-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36.valid-sudoku.cs
69 lines (62 loc) · 1.73 KB
/
36.valid-sudoku.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* @lc app=leetcode id=36 lang=csharp
*
* [36] Valid Sudoku
*/
// @lc code=start
public class Solution {
public bool IsValidSudoku(char[][] board) {
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
if(!checkBox(board, x, y)){
return false;
}
}
}
for(int i = 0; i < 9; i++){
if(!(checkRow(board, i) && checkCol(board, i))){
return false;
}
}
return true;
}
private bool checkRow(char[][] board, int row){
char[] count = new char[10];
for(int i = 0; i < 9; i++){
if(board[row][i] != '.'){
if(count[board[row][i] - '0'] > 0){
return false;
}
count[board[row][i] - '0']++;
}
}
return true;
}
private bool checkCol(char[][] board, int col){
char[] count = new char[10];
for(int i = 0; i < 9; i++){
if(board[i][col] != '.'){
if(count[board[i][col] - '0'] > 0){
return false;
}
count[board[i][col] - '0']++;
}
}
return true;
}
private bool checkBox(char[][] board, int x, int y){
char[] count = new char[10];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[x * 3 + i][y * 3 + j] != '.'){
if(count[board[x * 3 + i][y * 3 + j] - '0'] > 0){
return false;
}
count[board[x * 3 + i][y * 3 + j] - '0']++;
}
}
}
return true;
}
}
// @lc code=end