20240430 Finished

This commit is contained in:
jackfiled 2024-04-30 12:40:16 +08:00
parent 1b651de9be
commit 34b660e3a3
2 changed files with 79 additions and 1 deletions

View File

@ -116,4 +116,5 @@ mod p392_is_subsequence;
mod p167_two_sum_ii_input_array_is_sorted;
mod p209_minimum_size_subarray_sum;
mod p30_substring_with_concatenation_of_all_words;
mod p76_minimum_window_substring;
mod p76_minimum_window_substring;
mod p36_valid_sudoku;

View File

@ -0,0 +1,77 @@
/**
* [36] Valid Sudoku
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashSet;
impl Solution {
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
let mut set = HashSet::new();
for i in 0..9 {
for j in 0..9 {
if board[i][j] == '.' {
continue;
}
if set.contains(&board[i][j]) {
return false;
}
set.insert(board[i][j]);
}
set.clear();
}
for i in 0..9 {
for j in 0..9 {
if board[j][i] == '.' {
continue;
}
if set.contains(&board[j][i]) {
return false;
}
set.insert(board[j][i]);
}
set.clear();
}
for w in (0..9).step_by(3) {
for h in (0..9).step_by(3) {
for i in 0..3 {
for j in 0..3 {
if board[w + i][h + j] == '.' {
continue;
}
if set.contains(&board[w + i][h + j]) {
return false;
}
set.insert(board[w + i][h + j]);
}
}
set.clear();
}
}
true
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_36() {}
}