时间:2021-07-01 10:21:17 帮助过:153人阅读
递归算看看
递归算看看
请说下详细的算法啊
先给一个测试代码
暂不处理升序降序的情况
$ar = array('15836999992','158365111152','15836222712','15877889912','15866779912','15811122212','15855666212','15811122712','15823232312','15836368963','15811112569','158123478963','15843215698',);$mode = array( 'AAAAA' => '(\d)\\1{4}', 'AAAA' => '(\d)\\1{3}', 'AAA' => '(\d)\\1{2}', 'AABBCC' => '(\d)\\1(\d)\\2(\d)\\3', 'AABB' => '(\d)\\1(\d)\\2', 'AAABBB' => '(\d)\\1{2}(\d)\\2{2}', 'AABBB' => '(\d)\\1(\d)\\2{2}', 'AAABB' => '(\d)\\1{2}(\d)\\2', 'ABABAB' => '(\d)(\d)\\1\\2\\1\\2', 'ABAB' => '(\d)(\d)\\1\\2', 'AAAAB' => '(\d)\\1{3}\d',// '升序ABCD' => '(\d)\\1{4}',// '降序DCBA' => '(\d)\\1{4}',);foreach($ar as $tel) { foreach($mode as $m=>$p) { if($tel != preg_replace_callback("/$p/", 'back', $tel)) { $r[$tel][] = $m; } }}function back($r) { if(count($r) == 2 && strlen($r[0]) > 2) return ''; if(count($r) == 3 && $r[1] == $r[2]-1) return ''; if(count($r) == 4 && $r[1] == $r[2]-1 && $r[1] == $r[3]-2) return ''; return $r[0];}print_r($r);
Array( [15836999992] => Array ( [0] => AAAAA [1] => AAAA [2] => AAA [3] => AAAAB ) [158365111152] => Array ( [0] => AAAA [1] => AAA [2] => AAAAB ) [15836222712] => Array ( [0] => AAA ) [15877889912] => Array ( [0] => AABBCC [1] => AABB ) [15866779912] => Array ( [0] => AABB ) [15811122212] => Array ( [0] => AAA [1] => AABB [2] => AAABBB [3] => AABBB [4] => AAABB ) [15855666212] => Array ( [0] => AAA [1] => AABB [2] => AABBB ) [15811122712] => Array ( [0] => AAA [1] => AABB [2] => AAABB ) [15823232312] => Array ( [0] => ABABAB [1] => ABAB ) [15811112569] => Array ( [0] => AAAA [1] => AAA [2] => AAAAB ))
先给一个测试代码
暂不处理升序降序的情况
PHP code
$ar = array(
'15836999992',
'158365111152',
'15836222712',
'15877889912',
'15866779912',
'15811122212',
'15855666212',
'15811122712',
'15823232312',
'158……
那请问能否提供下升降序的思路也可以的
先给一个测试代码
暂不处理升序降序的情况
PHP code
$ar = array(
'15836999992',
'158365111152',
'15836222712',
'15877889912',
'15866779912',
'15811122212',
'15855666212',
'15811122712',
'15823232312',
'158……
'AAAAA' => '(\d)\\1{4}' 这段正则是什么意思呢
'AAAAA' => '(\d)\\1{4}' 这段正则是什么意思呢
(\d) 表示匹配到一个数字
\\1{4} 表示前面匹配到的数字要重复4次
于是他可以匹配
11111、22222、33333、44444、55555、66666、77777、88888、99999、00000
升降序不能用正则表示出来,需要另行判断
$ar = array('15811112569','158123478963','15843215698',);foreach($ar as $tel) { $u = 0; $d = 0; $t = str_split($tel); for($i=1; $i升序 158123478963= 3) { echo "升序 $tel\n"; break; } if($d >= 3) { echo "降序 $tel\n"; break; } }}
先给一个测试代码
暂不处理升序降序的情况
PHP code
$ar = array(
'15836999992',
'158365111152',
'15836222712',
'15877889912',
'15866779912',
'15811122212',
'15855666212',
'15811122712',
'15823232312',
'158……
那个正则 的 还是看的不太懂,说下原理吧
先给一个测试代码
暂不处理升序降序的情况
PHP code
$ar = array(
'15836999992',
'158365111152',
'15836222712',
'15877889912',
'15866779912',
'15811122212',
'15855666212',
'15811122712',
'15823232312',
'158……
尤其是back函数这块 没看懂
先给一个测试代码
暂不处理升序降序的情况
PHP code
$ar = array(
'15836999992',
'158365111152',
'15836222712',
'15877889912',
'15866779912',
'15811122212',
'15855666212',
'15811122712',
'15823232312',
'158……
此算法有点问题啊 如果是一个 15877998917这样的 应该也是AABB型的 但是他并不能分析出来哎
7楼不错,学习啦