-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
36 lines (29 loc) · 1.19 KB
/
solution.cpp
File metadata and controls
36 lines (29 loc) · 1.19 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
class Solution {
public:
int rotatedDigits(int n) {
int count = 0; // this will store the number of good numbers
for (int i = 1; i <= n; i++) {
int num = i;
bool isValid = true; // assume number is valid initially
bool hasChange = false; // to check if at least one digit changes
while (num > 0) {
int digit = num % 10; // extract last digit
// if digit is invalid after rotation
if (digit == 3 || digit == 4 || digit == 7) {
isValid = false;
break; // no need to check further
}
// if digit changes after rotation
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
hasChange = true;
}
num /= 10; // remove last digit
}
// count only if valid and has at least one changing digit
if (isValid && hasChange) {
count++;
}
}
return count;
}
};