1582. 二进制矩阵中的特殊位置

This commit is contained in:
li_chx 2026-03-04 09:10:10 +08:00
parent 563c0b3589
commit f8fd02dcb8
Signed by: li_chx
GPG Key ID: 70D4985BB8180E92
1 changed files with 45 additions and 36 deletions

View File

@ -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<Vec<i32>>) -> 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]]