[LeetCode] Count Number of Distinct Integers After Reverse Operations

2442. Count Number of Distinct Integers After Reverse Operations

You are given an array nums consisting of positive integers.

You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums.

Return the number of distinct integers in the final array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
long long reverse(int n) {
int res = 0;
while(n) {
res = res * 10 + n % 10;
n /= 10;
}
return res;
}
public:
int countDistinctIntegers(vector<int>& A) {
unordered_set<long long> us(begin(A), end(A));
for(int i = 0; i < A.size(); i++) {
us.insert(reverse(A[i]));
}

return us.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/16/PS/LeetCode/count-number-of-distinct-integers-after-reverse-operations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.