rustgym/leetcode/_260_single_number_3.rs
1struct Solution;
2
3impl Solution {
4 fn single_number(nums: Vec<i32>) -> Vec<i32> {
5 let mut bitmask = 0;
6 for &num in &nums {
7 bitmask ^= num;
8 }
9 let diff = bitmask & (-bitmask);
10 let mut x = 0;
11 for &num in &nums {
12 if diff & num != 0 {
13 x ^= num;
14 }
15 }
16 vec![x, bitmask ^ x]
17 }
18}
19
20#[test]
21fn test() {
22 let nums = vec![1, 2, 1, 3, 2, 5];
23 let res = vec![3, 5];
24 assert_eq!(Solution::single_number(nums), res);
25}