diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/count_distinct_int.rs | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/count_distinct_int.rs')
-rw-r--r-- | other/count_distinct_int.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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>) -> i32 { + let mut hs: HashSet<i32> = 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 +} |