From 6cbf838bd8baccc18f634b077b34773ea154c363 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 21 Dec 2023 21:36:18 +0800 Subject: [PATCH] 20231221 Finished --- src/solution/mod.rs | 1 + src/solution/s0052_n_queens_ii.rs | 89 +++++++++++++++++++++++++ src/solution/s0162_find_peak_element.rs | 2 +- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/solution/s0052_n_queens_ii.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 8e9a951..2d39d64 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -6,3 +6,4 @@ mod s0002_add_two_numbers; mod s0003_longest_substring_without_repeating_characters; mod s0162_find_peak_element; mod s2828_check_if_a_string_is_an_acronym_of_words; +mod s0052_n_queens_ii; diff --git a/src/solution/s0052_n_queens_ii.rs b/src/solution/s0052_n_queens_ii.rs new file mode 100644 index 0000000..45ea770 --- /dev/null +++ b/src/solution/s0052_n_queens_ii.rs @@ -0,0 +1,89 @@ +/** + * [52] N-Queens II + * + * The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. + * Given an integer n, return the number of distinct solutions to the n-queens puzzle. + * + * Example 1: + * + * Input: n = 4 + * Output: 2 + * Explanation: There are two distinct solutions to the 4-queens puzzle as shown. + * + * Example 2: + * + * Input: n = 1 + * Output: 1 + * + * + * Constraints: + * + * 1 <= n <= 9 + * + */ +pub struct Solution {} + +// problem: https://leetcode.cn/problems/n-queens-ii/ +// discuss: https://leetcode.cn/problems/n-queens-ii/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +use std::collections::VecDeque; + +impl Solution { + pub fn total_n_queens(n: i32) -> i32 { + let n = n as usize; + let mut result = 0; + let mut stack = VecDeque::new(); + + for i in 0..n { + stack.push_back((0usize, i, false)); + } + + let mut x_occupied = vec![false; n]; + let mut y_occupied = vec![false; 2 * n - 1]; + let mut z_occupied = vec![false; 2 * n - 1]; + + while let Some((x, y, flag)) = stack.pop_back() { + if flag { + x_occupied[y] = false; + y_occupied[n - 1 + x - y] = false; + z_occupied[x + y] = false; + } else { + x_occupied[y] = true; + y_occupied[n - 1 + x - y] = true; + z_occupied[x + y] = true; + + stack.push_back((x, y, true)); + + if x == n - 1 { + result += 1; + continue; + } + + for j in 0..n { + // 注意这里在判断的点是(x + 1, j) + if !x_occupied[j] && !y_occupied[n + x - j] && !z_occupied[x + 1 + j] { + stack.push_back((x + 1, j, false)); + } + } + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_52() { + assert_eq!(1, Solution::total_n_queens(1)); + assert_eq!(2, Solution::total_n_queens(4)); + assert_eq!(14200, Solution::total_n_queens(12)); + } +} diff --git a/src/solution/s0162_find_peak_element.rs b/src/solution/s0162_find_peak_element.rs index 046f995..42d2a1c 100644 --- a/src/solution/s0162_find_peak_element.rs +++ b/src/solution/s0162_find_peak_element.rs @@ -34,7 +34,7 @@ pub struct Solution {} impl Solution { pub fn find_peak_element(nums: Vec) -> i32 { if nums.len() == 1 { - 0 + return 0; } let (mut left, mut right) = (0, nums.len() - 1);