From f8fd02dcb8f6558c3d9b9cb4640f3d8d97fd79db Mon Sep 17 00:00:00 2001 From: li_chx Date: Wed, 4 Mar 2026 09:10:10 +0800 Subject: [PATCH] =?UTF-8?q?1582.=20=E4=BA=8C=E8=BF=9B=E5=88=B6=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E4=B8=AD=E7=9A=84=E7=89=B9=E6=AE=8A=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 81 +++++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index f016ff4..7ce79f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,48 +1,57 @@ -use std::cell::RefCell; - struct Solution; mod arr; impl Solution { - pub fn find_kth_bit(n: i32,mut k: i32) -> char { - let len = Self::get_length_arr()(n); - if n == 1 || k == 0 { - return '0'; - } - if k == len / 2 + 1 { - return '1'; - } - if k > len / 2 + 1 { - if Self::find_kth_bit(n - 1, (len / 2+1) - (k - (len / 2 + 1))) == '1' { - '0' - } else { - '1' - } - } else { - Self::find_kth_bit(n - 1, k) - } - } - fn get_length_arr() -> impl Fn(i32) -> i32 { - let cell = RefCell::new(None); - move |n| { - let mut value = cell.borrow_mut(); - if value.is_none() { - let mut arr = Vec::with_capacity(20); - arr.push(1); - for _ in 1..20 { - arr.push(arr[arr.len() - 1] * 2 + 1); + pub fn num_special(mat: Vec>) -> i32 { + let mut disable = vec![false; mat[0].len()]; + let mut arr = vec![0; mat[0].len()]; + for i in 0..mat.len() { + let mut disable_i = false; + let mut next_do_disable = false; + let mut last_index = 1000_usize; + for j in 0..mat[0].len() { + if mat[i][j] == 1 { + if next_do_disable { + disable_i = true; + arr[last_index] = 0; + disable[last_index] = true; + disable[j] = true; + arr[j] = 0; + } + if disable[j] { + disable_i = true; + continue; + } + if disable_i { + disable[j] = true; + if arr[j] != 0 { + arr[j] = 0; + } + continue; + } + if arr[j] != 0 { + disable[j] = true; + arr[j] = 0; + disable_i = true; + continue + } + arr[j] += 1; + next_do_disable = true; + last_index = j; } - *value = Some(arr); - } - if let Some(x) = value.as_ref() { - x[n as usize - 1] - } else { - 0 } } + arr.iter().sum() } } fn main() { - let result = Solution::find_kth_bit(4, 14); + let result = Solution::num_special(arr::make_matrix("[[0,0,0,0,0,0],[0,0,1,0,0,0],[0,0,0,0,1,0],[0,0,0,0,0,0],[0,0,1,0,0,1],[0,0,0,0,0,0],[0,0,0,0,0,0]]")); println!("{:?}", result); } +// [[0,0,0,0,0,0], +// [0,0,1,0,0,0], +// [0,0,0,0,1,0], +// [0,0,0,0,0,0], +// [0,0,1,0,0,1], +// [0,0,0,0,0,0], +// [0,0,0,0,0,0]] \ No newline at end of file