第一题给一个int array, 找其中最长连续递增sub array的长度。比如[1, 2, 3, 1, 4],结果就应该是[1, 2, 3]的长度,为3。Two pointer
public static void main(String[] args) {
getMaxSubArray(new int[]{1, 56, 58, 57, 90, 92, 94, 93, 91, 45});
getMaxSubArray(new int[]{1, 56, 58, 57, 90, 92, 94, 95, 91, 45});
}
static int getMaxSubArray(int[] array) {
int max = 1;
int start = 0;
int aux = 0;
int count = 1;
for (int i = 1; i < array.length; i++) {
if (array[i] > array[i - 1]) {
count++;
} else {
aux = i;
count = 1;
}
if (count > max) {
max = count;
start = aux;
}
}
System.out.println(max);
System.out.println("Starting index = "+start+" Ending index = "+(start + max - 1));
return max;
}