From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/count_distinct_int.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 other/count_distinct_int.rs (limited to 'other/count_distinct_int.rs') diff --git a/other/count_distinct_int.rs b/other/count_distinct_int.rs new file mode 100644 index 0000000..32ce622 --- /dev/null +++ b/other/count_distinct_int.rs @@ -0,0 +1,35 @@ +//2442. Count Number of Distinct Integers After Reverse Operations +use std::collections::HashSet; +struct Solution; + +/* + * array nums contains positive integers + * take each integer in array and reverse its digits (eg. 13 -> 31) + * then *add* it to end of array. return number of distinct integers. + * [1,13,10,12,31] becomes [1,13,10,12,31,1,31,1,21,13] + * and total num of distinct ints = 6 + */ + +impl Solution { + pub fn count_distinct_integers(nums: Vec) -> i32 { + let mut hs: HashSet = nums.iter().map(|&n| Self::reverse(n)).collect(); + hs.extend(nums.iter()); + hs.len() as i32 + } + fn reverse(mut n: i32) -> i32 { + let mut rev: i32 = 0; + while n > 0 { + rev *= 10; + rev += n % 10; + n /= 10; + } + rev + } +} + +fn main() { + let nums1 = vec![1,13,10,12,31]; + let nums2 = vec![2,2,2]; + println!("{}", Solution::count_distinct_integers(nums1)); //expect: 6 + println!("{}", Solution::count_distinct_integers(nums2)); //expect: 1 +} -- cgit v1.2.3