62,616
社区成员
发帖
与我相关
我的任务
分享
private static int getMaxContinuousNum(int[] a) {
Arrays.sort(a);
int tempCount = 1;
int max=0;
System.out.println(Arrays.toString(a));
for (int i = 0; i < a.length - 1; i++) {
if (a[i + 1] - a[i] == 1) {
tempCount++;
} else {
if(tempCount>max){
max=tempCount;
tempCount=1;
}
else{
tempCount=1;
}
}
}
if(tempCount>max){
max=tempCount;
}
return max;
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
class Test {
public static void main(String[] args) {
Integer[] num=new Integer[15];
Set<Integer> s=new HashSet<Integer>();
while(s.size()<15){
s.add((int)(Math.random()*33));
}
num=s.toArray(num);
Arrays.sort(num);
System.out.println(Arrays.toString(num));
int max=0;
int count=1;
for(int i=1;i<num.length;i++){
if(num[i]-num[i-1]==1){
count++;
}else if(count>max){
max=count;
count=1;
}else{
count=1;
}
}
System.out.println(max);
}
}