diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2733115..06b3074 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -186,4 +186,5 @@ mod p295_find_median_from_data_stream; mod p67_add_binary; mod p190_reverse_bits; mod p191_number_of_1_bits; -mod p136_single_number; \ No newline at end of file +mod p136_single_number; +mod p137_single_number_ii; \ No newline at end of file diff --git a/src/problem/p137_single_number_ii.rs b/src/problem/p137_single_number_ii.rs new file mode 100644 index 0000000..859fe98 --- /dev/null +++ b/src/problem/p137_single_number_ii.rs @@ -0,0 +1,32 @@ +/** + * [137] Single Number II + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn single_number(nums: Vec) -> i32 { + let (mut a, mut b) = (0, 0); + + for i in nums { + b = !a & (b ^ i); + a = !b & ( a ^ i); + } + + b + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_137() { + assert_eq!(1, Solution::single_number(vec![1, 2, 2, 2])); + } +}