-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontest1284.java
More file actions
61 lines (59 loc) · 2 KB
/
contest1284.java
File metadata and controls
61 lines (59 loc) · 2 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
import java.util.*;
public class contest1284 {
public static void main(String[] args){
System.out.println(numDupDigitsAtMostN(100));
}
public static int numDupDigitsAtMostN(int N) {
if(N < 100){
if(N >= 99){
return 9;
}else if(N >= 88){
return 8;
}else if(N >= 77){
return 7;
}else if(N >= 66){
return 6;
}else if(N >= 55){
return 5;
}else if(N >= 44){
return 4;
}else if(N >= 33){
return 3;
}else if(N >= 22){
return 2;
}else if(N >= 11){
return 1;
}else {
return 0;
}
}
List<String> newDigitList = Arrays.asList(new String[]{"00","11","22","33","44","55","66","77","88","99"});
List<String> oldDigitList = new ArrayList<>();
Set<String> set = new HashSet<>();
for(int digit = 3; digit <= 9; digit ++){
if(digit > (""+N).length()){
break;
}
oldDigitList = newDigitList;
newDigitList = new ArrayList<>();
for(int i = 0; i < oldDigitList.size(); i++){
String cur = oldDigitList.get(i);
for(int j = 0; j <= cur.length(); j++){
for(int k = 0; k <= 9; k++){
String newS = cur.substring(0,j) + k + cur.substring(j,cur.length());
if(newS.startsWith("0")) {
newDigitList.add(newS);
}else{
if(Integer.parseInt(newS) <= N){
set.add(newS);
System.out.println(newS);
newDigitList.add(newS);
}
}
}
}
}
}
return 9 + set.size();
}
}