Skip to content

Commit

Permalink
leetcode: Add 0260
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 22, 2024
1 parent c2d63cc commit 6b513d0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0260.single-number-iii/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0260-single-number-iii"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0260.single-number-iii/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
64 changes: 64 additions & 0 deletions src/leetcode/0260.single-number-iii/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

// bit-manipulation
// XOR
pub fn single_number1(nums: Vec<i32>) -> Vec<i32> {
// xor 是 a1 和 a2 共有的比特位
let mut xor: i32 = 0;
for &num in &nums {
xor ^= num;
}

let mut k: i32 = 1;
loop {
if (xor & k) != 0 {
break;
}
k *= 2;
}

let mut a1: i32 = 0;
let mut a2: i32 = 0;
for &num in &nums {
if (num & k) == 0 {
a1 ^= num;
} else {
a2 ^= num;
}
}

// 排序
if a1 > a2 {
(a1, a2) = (a2, a1);
}
vec![a1, a2]
}

pub type SolutionFn = fn(Vec<i32>) -> Vec<i32>;

fn check_solution(func: SolutionFn) {
let nums = vec![1, 2, 1, 3, 2, 5];
assert_eq!(func(nums), [3, 5]);

let nums = vec![-1, 0];
assert_eq!(func(nums), [-1, 0]);

let nums = vec![0, 1];
assert_eq!(func(nums), [0, 1]);
}

fn main() {
check_solution(single_number1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, single_number1};

#[test]
fn test_single_number1() {
check_solution(single_number1);
}
}

0 comments on commit 6b513d0

Please sign in to comment.