From e4aa6c1c8f58856f198d5dc631c2d9a9dc929948 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 27 Jul 2024 10:57:47 +0800 Subject: [PATCH] 20240727 Finished --- src/problem/mod.rs | 3 ++- src/problem/p136_single_number.rs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/problem/p136_single_number.rs 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])); + } +}