public class BitArray {
private static byte[] bits = new byte[16];
public static void set(int index) {
bits[index] = (byte)(1);
}
public static byte get(int index) {
return bits[index];
}
public static int[] ComputeMaxLargestRangeSubListFirstAndLastItem(int[] array) {
for (int i = 0; i < array.length; i++) {
int item = array[i];
set(item);
}
int maxLength = 0;
int tmpLength = 0;
int[] data = new int[2];
int[] tmpData = new int[2];
for(int i=0; i < bits.length; i++) {
byte b = bits[i];
if(b == 1){
tmpLength++;
if(tmpLength == 1) {
tmpData[0] = i;
} else {
tmpData[1] = i;
}
} else {
tmpLength = 0;
}
if (tmpLength > maxLength) {
maxLength = tmpLength;
data[0] = tmpData[0];
data[1] = tmpData[1];
}
}
return data;
}
public static void main(String[] args) {
int[] array = new int[]{1, 11, 3, 0, 15, 5, 2, 4, 10, 7, 12, 6};
int[] data = ComputeMaxLargestRangeSubListFirstAndLastItem(array);
System.out.println(data[0]);
System.out.println(data[1]);
}
}
BitArray找出数组中的最大连续数列的首位和末位数
最新推荐文章于 2024-09-08 15:12:17 发布