Skip to content

Commit 09ac214

Browse files
committed
7-19-2020
1 parent 36acf55 commit 09ac214

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/_0036_valid_sudoku.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
5+
let mut row_checker: Vec<Vec<bool>> = vec![vec![false; 9]; 9];
6+
let mut col_checker: Vec<Vec<bool>> = vec![vec![false; 9]; 9];
7+
let mut box_checker: Vec<Vec<bool>> = vec![vec![false; 9]; 9];
8+
9+
for i in 0..board.len() {
10+
for j in 0..board[0].len() {
11+
let character: char = board[i][j];
12+
if character == '.' {
13+
continue;
14+
}
15+
let number: usize = character as usize - b'1' as usize;
16+
if row_checker[i][number] {
17+
return false;
18+
} else {
19+
row_checker[i][number] = true;
20+
}
21+
if col_checker[j][number] {
22+
return false;
23+
} else {
24+
col_checker[j][number] = true;
25+
}
26+
let k: usize = (i / 3) * 3 + j / 3;
27+
if box_checker[k][number] {
28+
return false;
29+
} else {
30+
box_checker[k][number] = true;
31+
}
32+
}
33+
}
34+
true
35+
}
36+
}
37+
38+
#[test]
39+
fn test() {
40+
let board = vec_vec_char![
41+
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
42+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
43+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
44+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
45+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
46+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
47+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
48+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
49+
['.', '.', '.', '.', '8', '.', '.', '7', '9']
50+
];
51+
assert_eq!(Solution::is_valid_sudoku(board), true);
52+
let board = vec_vec_char![
53+
['8', '3', '.', '.', '7', '.', '.', '.', '.'],
54+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
55+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
56+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
57+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
58+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
59+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
60+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
61+
['.', '.', '.', '.', '8', '.', '.', '7', '9']
62+
];
63+
assert_eq!(Solution::is_valid_sudoku(board), false);
64+
}

src/_0078_subsets.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
5+
let mut result: Vec<Vec<i32>> = vec![];
6+
let mut current: Vec<i32> = vec![];
7+
let n: usize = nums.len();
8+
Self::dfs(&nums, 0, &n, &mut result, &mut current);
9+
result
10+
}
11+
12+
fn dfs(
13+
nums: &Vec<i32>,
14+
i: usize,
15+
n: &usize,
16+
result: &mut Vec<Vec<i32>>,
17+
current: &mut Vec<i32>,
18+
) {
19+
if i == *n {
20+
result.push(current.to_vec());
21+
} else {
22+
Self::dfs(nums, i + 1, n, result, current);
23+
current.push(nums[i]);
24+
Self::dfs(nums, i + 1, n, result, current);
25+
current.pop();
26+
}
27+
}
28+
}
29+
30+
#[test]
31+
fn test() {
32+
assert_eq!(
33+
Solution::subsets(vec![1, 2, 3]),
34+
vec_vec_i32![[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
35+
);
36+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ mod _0022_generate_parentheses;
2828
//
2929
mod _0034_find_first_and_last_position_of_element_in_sorted_array;
3030
//
31+
mod _0036_valid_sudoku;
32+
//
3133
mod _0046_permutations;
3234
//
3335
mod _0053_maximum_subarray;
@@ -40,6 +42,8 @@ mod _0067_add_binary;
4042
//
4143
mod _0070_climbing_stairs;
4244
//
45+
mod _0078_subsets;
46+
//
4347
mod _0083_remove_duplicates_from_sorted_list;
4448
//
4549
mod _0088_merge_sorted_array;

0 commit comments

Comments
 (0)