2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,41 +1,22 @@
|
|||
public class Range{
|
||||
public static void main(String[] args){
|
||||
System.out.println(compress2Range("-6, -3, -2, -1, 0, 1, 3, 4, 5, 7," +
|
||||
" 8, 9, 10, 11, 14, 15, 17, 18, 19, 20"));
|
||||
System.out.println(compress2Range(
|
||||
"0, 1, 2, 4, 6, 7, 8, 11, 12, 14, " +
|
||||
"15, 16, 17, 18, 19, 20, 21, 22, 23, 24," +
|
||||
"25, 27, 28, 29, 30, 31, 32, 33, 35, 36," +
|
||||
"37, 38, 39"));
|
||||
}
|
||||
public class RangeExtraction {
|
||||
|
||||
private static String compress2Range(String expanded){
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] nums = expanded.replace(" ", "").split(",");
|
||||
int firstNum = Integer.parseInt(nums[0]);
|
||||
int rangeSize = 0;
|
||||
for(int i = 1; i < nums.length; i++){
|
||||
int thisNum = Integer.parseInt(nums[i]);
|
||||
if(thisNum - firstNum - rangeSize == 1){
|
||||
rangeSize++;
|
||||
}else{
|
||||
if(rangeSize != 0){
|
||||
result.append(firstNum).append((rangeSize == 1) ? ",": "-")
|
||||
.append(firstNum+rangeSize).append(",");
|
||||
rangeSize = 0;
|
||||
}else{
|
||||
result.append(firstNum).append(",");
|
||||
}
|
||||
firstNum = thisNum;
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
|
||||
37, 38, 39};
|
||||
|
||||
int len = arr.length;
|
||||
int idx = 0, idx2 = 0;
|
||||
while (idx < len) {
|
||||
while (++idx2 < len && arr[idx2] - arr[idx2 - 1] == 1);
|
||||
if (idx2 - idx > 2) {
|
||||
System.out.printf("%s-%s,", arr[idx], arr[idx2 - 1]);
|
||||
idx = idx2;
|
||||
} else {
|
||||
for (; idx < idx2; idx++)
|
||||
System.out.printf("%s,", arr[idx]);
|
||||
}
|
||||
}
|
||||
if(rangeSize != 0){
|
||||
result.append(firstNum).append((rangeSize == 1) ? "," : "-").
|
||||
append(firstNum + rangeSize);
|
||||
rangeSize = 0;
|
||||
} else {
|
||||
result.append(firstNum);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue