forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0036-valid-sudoku.rs
49 lines (40 loc) · 1.28 KB
/
0036-valid-sudoku.rs
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
use std::collections::HashSet;
impl Solution {
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut row: HashSet<char> = HashSet::new();
let mut col: HashSet<char> = HashSet::new();
let mut bx : HashSet<char> = HashSet::new();
for i in 0..9{
for j in 0..9{
let r = board[i][j];
let c = board[j][i];
let b = board[i / 3 * 3 + j / 3][i/3 * 3 + j%3];
if r != '.'{
if !row.contains(&r){
row.insert(r);
}else{
return false;
}
}
if c != '.'{
if !col.contains(&c){
col.insert(c);
}else{
return false;
}
}
if b != '.'{
if !bx.contains(&b){
bx.insert(b);
}else{
return false;
}
}
}
row.clear();
col.clear();
bx.clear();
}
true
}
}