-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (27 loc) · 1 KB
/
Copy pathsolution.java
File metadata and controls
34 lines (27 loc) · 1 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
class Solution {
public int rotatedDigits(int n) {
int count = 0; // total good numbers
for (int i = 1; i <= n; i++) {
int num = i;
boolean isValid = true; // assume valid
boolean hasChange = false; // check if it changes
while (num > 0) {
int digit = num % 10; // extract last digit
// invalid digits
if (digit == 3 || digit == 4 || digit == 7) {
isValid = false;
break;
}
// digits that change
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
hasChange = true;
}
num /= 10; // remove last digit
}
if (isValid && hasChange) {
count++;
}
}
return count;
}
}