-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDistinctIntegers-LCQ.cpp
More file actions
52 lines (39 loc) · 1.38 KB
/
CountDistinctIntegers-LCQ.cpp
File metadata and controls
52 lines (39 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
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.
Example 1:
Input: nums = [1,13,10,12,31]
Output: 6
Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13].
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).
Example 2:
Input: nums = [2,2,2]
Output: 1
Explanation: After including the reverse of each number, the resulting array is [2,2,2,2,2,2].
The number of distinct integers in this array is 1 (The number 2).
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 106
*/
class Solution {
public:
int rev(int n){
int r=0;
while(n>0){
r=r*10+(n%10);
n/=10;
}
return r;
}
int countDistinctIntegers(vector<int>& nums) {
set<int>res;
for(int ele:nums){
res.insert(ele);
res.insert(rev(ele));
}
return res.size();
}
};