Last active
December 21, 2015 16:09
-
-
Save guweigang/6331876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function seq($width, $lastNum, $inc) | |
{ | |
$num = $lastNum; | |
if($inc==true) { | |
if($lastNum+$width>10) {return false;} | |
} else { | |
if($lastNum-$width<0) {return false;} | |
} | |
$num = 0; | |
for($i=0;$i<$width;++$i) { | |
if($inc) { | |
$nextNum = $lastNum + $i; | |
} else { | |
$nextNum = $lastNum - $i; | |
} | |
$num += pow(10, $i) * $nextNum; | |
} | |
return $num; | |
} | |
function match($ID, $minWidth) { | |
$ID = intval($ID); | |
$width = ceil(log10($ID)); | |
for($i=$width;$i>=$minWidth;--$i) { | |
for($j=0; $width-$j>=$i;++$j) { | |
$ID2 = intval($ID / pow(10, $j)); | |
$widthNum = $ID2 % pow(10, $i); | |
$lastNum = $widthNum % 10; | |
$inc = seq($i, $lastNum, true); | |
$des = seq($i, $lastNum, false); | |
if($widthNum === $inc || $des === $widthNum) { | |
return $widthNum; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment