-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path788.java
More file actions
67 lines (57 loc) · 1.92 KB
/
788.java
File metadata and controls
67 lines (57 loc) · 1.92 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
int[] goods = {2, 5, 6, 9};
int[] valids = {0, 1, 2, 5, 6, 8, 9};
public int rotatedDigits(int N) {
int n = N;
int digitCount = 0;
while (n > 0) {
n /= 10;
digitCount++;
}
int count = 0;
for (int len = 1; len <= digitCount; len++) {
int[] num = new int[digitCount];
count += getCount(0, 0, false, N, len);
}
return count;
}
public int getCount(int num, int i, boolean hasGoodSeen, int N, int len) {
if (i == len && hasGoodSeen) {
return 1;
}
if (num > N) return 0;
int count = 0;
for (int n: valids) {
boolean nextGoodSeen = hasGoodSeen || n == 2 || n == 5 || n == 6 || n == 9;
int nextNum = num * 10 + n;
if (nextNum == 0) continue;
if (nextNum > N) break;
count += getCount (nextNum, i + 1, nextGoodSeen, N, len);
}
return count;
}
}
__________________________________________________________________________________________________
sample 31584 kb submission
class Solution {
public int rotatedDigits(int N) {
int ret = 0;
for (int i=1; i<=N; i++) {
if (rotateAble(i)) ret++;
}
return ret;
}
private boolean rotateAble(int num) {
boolean changed = false;
while (num > 0) {
int res = num % 10;
if (res == 3 || res == 4 || res == 7) return false;
if (res == 2 || res == 5 || res == 6 || res == 9) changed = true;
num /= 10;
}
return changed;
}
}
__________________________________________________________________________________________________