2125. 银行中的激光束数量
This commit is contained in:
parent
292f597d2b
commit
4f601269e8
35
src/main.rs
35
src/main.rs
|
|
@ -2,32 +2,25 @@ mod arr;
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn get_sneaky_numbers(nums: Vec<i32>) -> Vec<i32> {
|
pub fn number_of_beams(bank: Vec<String>) -> i32 {
|
||||||
let n = nums.len() - 2;
|
let mut ans = 0;
|
||||||
let xor_all: i32 = nums.iter().enumerate().fold((n ^ (n+1)) as i32, |acc, (i ,&x)| acc ^ x ^ i as i32);
|
let mut last_ones = 0;
|
||||||
let mut idx = 0;
|
for i in 0..bank.len() {
|
||||||
let mut calc_idx = xor_all;
|
let new_ones = bank[i].chars().fold(0,|last, c| last + if c == '1'{1}else {0});
|
||||||
while true {
|
if new_ones == 0 {
|
||||||
if calc_idx % 2 == 1 {
|
continue;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
calc_idx >>= 1;
|
ans += last_ones * new_ones;
|
||||||
idx += 1;
|
last_ones = new_ones;
|
||||||
}
|
|
||||||
let mut ans = vec![0;2];
|
|
||||||
for (i,&x) in nums.iter().enumerate() {
|
|
||||||
if i < n{
|
|
||||||
ans[i >> idx &1 as usize] ^= i as i32;
|
|
||||||
}
|
|
||||||
ans[(x >> idx &1) as usize] ^= x;
|
|
||||||
}
|
}
|
||||||
ans
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
let src = vec!["011001", "000000", "010100", "001000"]
|
||||||
"{:?}",
|
.iter()
|
||||||
Solution::get_sneaky_numbers(vec![2,0,2,1,0])
|
.map(|s| s.to_string())
|
||||||
);
|
.collect();
|
||||||
|
println!("{:?}", Solution::number_of_beams(src));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue