Open
Description
// Time: O(n)
// Space: O(1)
pub struct Solution {}
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let (one, _) = nums.iter().fold((0, 0), |(one, two), num| {
(
(!num & one) | (num & !one & !two),
(!num & two) | (num & one),
)
});
one
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(Solution::single_number(vec![2, 2, 3, 2]), 3);
assert_eq!(Solution::single_number(vec![0, 1, 0, 1, 0, 1, 99]), 99);
assert_eq!(Solution::single_number(vec![0, 0, 0, 1, 1, 1, 5]), 5);
}
}
Metadata
Metadata
Assignees
Labels
No labels