如需求:最高连续打卡多少天? 或者一串数字,最大连续数字有多少个。
$nums = [\'2018-10-09\',\'2018-10-10\',\'2018-10-11\',\'2018-10-13\',\'2018-10-15\',\'2018-10-16\',\'2018-10-17\',\'2018-10-18\'];$max = 0;$hash = [];foreach ($nums as $val) {$hash[$val] = 1;}foreach ($nums as $val){$num = 1;if (!$hash[$val - 1]) {while($hash[++$val]) $num++;}$max = $num > $max ? $num : $max;}echo $max;
$nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1];$max = 0;$hash = [];foreach ($nums as $val) {$hash[$val] = 1;}foreach ($nums as $val){$num = 1;if (!$hash[$val - 1]) {while($hash[++$val]) $num++;}$max = $num > $max ? $num : $max;}echo $max;