问题描述

å对于一个无序的数列,求出里面最大的连续子数列

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$arr = array(1,9,5,5,6,7,0,9,4,3,4,5,6,3,0,1);

$max = array();
$tem = array();
for($i=1; $i<count($arr); $i++){
if(($arr[$i]-1) == $arr[$i-1]){
if(!count($tem)) {
$tem[] = $arr[$i-1];
}
$tem[] = $arr[$i];
}else{
if(count($max) < count($tem)) {
$max = array();
$max = $tem;
}
$tem = array();
}
}

var_dump($max);

时间复杂度

1
O(n)