-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.java
More file actions
37 lines (30 loc) · 1006 Bytes
/
12.java
File metadata and controls
37 lines (30 loc) · 1006 Bytes
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
// Q12.Numbers At Most N Given Digit Set
public class Solution {
public int atMostNGivenDigitSet(String[] d, int n) {
String s = String.valueOf(n);
int len = s.length(), b = d.length, t = 0;
for (int i = 1; i < len; i++)
t += Math.pow(b, i);
for (int i = 0; i < len; i++) {
boolean f = false;
char c = s.charAt(i);
for (String x : d) {
char y = x.charAt(0);
if (y < c) {
t += Math.pow(b, len - i - 1);
} else if (y == c) {
f = true;
break;
} else {
break;
}
}
if (!f) return t;
}
return t + 1;
}
public static void main(String[] args) {
Solution o = new Solution();
System.out.println(o.atMostNGivenDigitSet(new String[]{"1","3","5","7"}, 100));
}
}