-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumsAtMostN_902.java
62 lines (54 loc) · 2.02 KB
/
NumsAtMostN_902.java
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
package org.example;
public class NumsAtMostN_902 {
static class Solution {
public int atMostNGivenDigitSet(String[] digits, int n) {
return calculate(digits,n,true);
}
private int calculate(String[] digits, int n, boolean isOrigin){
if (n==0){
return 0;
}
int length = ((int)Math.log10(n))+1;
System.out.println(n);
System.out.println(length);
int[] digitsInt = new int[digits.length];
for (int i=0;i<digits.length;i++){
digitsInt[i] = Integer.parseInt(digits[i]);
}
int digitLength = digits.length;
int result = 0;
if (isOrigin){
for (int i=1;i<length;i++){
result += (int) Math.pow(digitLength,i);
}
}
// get the starting digit
int largePart = (int) Math.pow(10, length-1);
int startingDigit = (int) n/largePart;
for (int i=0;i<digits.length;i++){
if (digitsInt[i]<startingDigit){
result += (int) Math.pow(digitLength,length-1);
}
// if equal
else if (digitsInt[i]==startingDigit) {
if (n<10){
if (digitsInt[i]<=n){
result += 1;
}
System.out.println("29line: "+result);
}
else if ((n - largePart*startingDigit)<=largePart/10) {
result += calculate(digits, n - largePart*startingDigit, false);
System.out.println("33line: "+result);
}
}
}
return result;
}
}
public static void main(String[] args) {
Solution s = new Solution();
int result = s.atMostNGivenDigitSet(new String[]{"1","4","9"},1000000000);
System.out.println(result);
}
}