1- 设当前号码是 currentNumber = false;
2- 设当前号码连续出现次数是 currentNumberTimes = 1;
3- 设最大连续出现次数是 maxTimes = 0;
4- 设最大连续出现次数对应的数字是 maxTimesNumber = false;
5- 将数字转为字符串,顺序读取每一位,该位的值存入变量 n
6- 将 n 和 currentNumber对比,如果一致 currentNumberTimes ++,否则:
6.1 用currentNumberTimes和maxTimes对比,如果大于maxTimes, maxTimes = currentNumberTimes; maxTimesNumber = currentNumber
6.2 currentNumber = n,currentNumberTimes = 1
7- maxTimesNumber为所求数字, maxTimes为连续出现的次数
注:如果出现相同长度的连续数字,取值为第一个,如 111222,maxTimesNumber = 1, maxTimes = 3
$number = '43124321333334124233343223900';
$currentNumber = false;
$currentNumberTimes = 1;
$maxTimes = 0;
$maxTimesNumber = false;
for ($i = 0, $l = strlen($number); $i < $l; $i ++ ) {
$n = $number[$i];
if ($n === $currentNumber) {
$currentNumberTimes ++;
} else {
if ($currentNumberTimes > $maxTimes) {
$maxTimes = $currentNumberTimes;
$maxTimesNumber = $currentNumber;
}
$currentNumberTimes = 1;
$currentNumber = $n;
}
}
echo "数字{$maxTimesNumber}连续出现了{$maxTimes}次。\n";
//数字3连续出现了5次。