-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.valid sudoku..cpp
More file actions
53 lines (52 loc) · 1.43 KB
/
36.valid sudoku..cpp
File metadata and controls
53 lines (52 loc) · 1.43 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
// check rows
for(int i=0;i<9;i++){
map<char,int> m;
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
if(m[board[i][j]]==0){
m[board[i][j]]++;
}else{
return false;
}
}
}
}
// check cols
for(int i=0;i<9;i++){
map<char,int> m;
for(int j=0;j<9;j++){
if(board[j][i]!='.'){
if(m[board[j][i]]==0){
m[board[j][i]]++;
}else{
return false;
}
}
}
}
// check boxes 3x3
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
map<char,int> m;
for(int x=i;x<i+3;x++){
for(int y=j;y<j+3;y++){
if(board[x][y]!='.'){
if(m[board[x][y]]==0){
m[board[x][y]]++;
}else{
return false;
}
}
}
}
}
}
return true;
}
};