-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequentialDigits.java
More file actions
46 lines (38 loc) · 1.25 KB
/
SequentialDigits.java
File metadata and controls
46 lines (38 loc) · 1.25 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
import java.util.ArrayList;
import java.util.List;
public class SequentialDigits {
public static void main(String[] args) {
System.out.println(sequentialDigits(100, 300));
}
public static List<Integer> sequentialDigits(int low, int high) {
String lowValue = String.valueOf(low);
String highValue = String.valueOf(high);
int lowLen = lowValue.length();
int upperLen = highValue.length();
String sample = "123456789";
List<Integer> result = new ArrayList<>();
for(int length = lowLen; length <= upperLen; length++)
{
int i = 0;
StringBuilder str = new StringBuilder();
for(int j = 0; j < sample.length(); j++)
{
str.append(sample.charAt(j));
if(j - i + 1 > length)
{
str.deleteCharAt(0);
i++;
}
if(j - i + 1 == length)
{
int value = Integer.parseInt(str.toString());
if(value >= low && value <= high)
{
result.add(value);
}
}
}
}
return result;
}
}