I'm having trouble breaking down the double-mask bitwise ops approach. Here's one ripped from Leetcode's best runtime solutions:
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let mut ones = 0;
let mut twos = 0;
for n in nums {
ones = (ones ^ n) & !twos;
twos = (twos ^ n) & !ones;
}
ones
}
}
Something about how one mask prevent certain bits from being set in the other, but their intermittent values are throwing me off. I don't really understand what they're supposed to concretely represent, or how this approach was derived.
I'm having trouble breaking down the double-mask bitwise ops approach. Here's one ripped from Leetcode's best runtime solutions:
Something about how one mask prevent certain bits from being set in the other, but their intermittent values are throwing me off. I don't really understand what they're supposed to concretely represent, or how this approach was derived.