From 292f597d2b5819af8e31409b3d1715b784408e1a Mon Sep 17 00:00:00 2001 From: li-chx Date: Fri, 31 Oct 2025 16:52:13 +0800 Subject: [PATCH] =?UTF-8?q?3289.=20=E6=95=B0=E5=AD=97=E5=B0=8F=E9=95=87?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=8D=A3=E8=9B=8B=E9=AC=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c61d486..32751e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,32 @@ mod arr; struct Solution {} impl Solution { - pub fn min_number_operations(target: Vec) -> i32 { - target[0] + target.windows(2).map(|x| (x[1] - x[0]).max(0)).sum::() + pub fn get_sneaky_numbers(nums: Vec) -> Vec { + let n = nums.len() - 2; + let xor_all: i32 = nums.iter().enumerate().fold((n ^ (n+1)) as i32, |acc, (i ,&x)| acc ^ x ^ i as i32); + let mut idx = 0; + let mut calc_idx = xor_all; + while true { + if calc_idx % 2 == 1 { + break; + } + calc_idx >>= 1; + idx += 1; + } + 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 } } fn main() { - println!("{:?}", Solution::min_number_operations(vec![3,1,5,4,2])); + println!( + "{:?}", + Solution::get_sneaky_numbers(vec![2,0,2,1,0]) + ); }