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