Home > Backend Development > PHP Tutorial > 获得数组里的数字连号问题

获得数组里的数字连号问题

WBOY
Release: 2016-06-20 12:42:41
Original
1330 people have browsed it

$result = array(
  '1,3,5,6,7,8,9,10,14,15,18,100,101',
  '1,2,5,6,7,8,9,10,14,15,18,100,101,102'
    );
求一个函数能计算数字元素包含连号的类型与次数
例如:第一组: 6连号1组,2连号2组
            第一组: 6连号1组,2连号2组,3连号1组
我目前程序只能做到 1,2,5,6,7,8,9,10,  认为这是2连号6组!想了好久


回复讨论(解决方案)

放到数据库里,用自连接轻松搞定

楼上倒是给语句方案啊

放到数据库里,用自连接轻松搞定


没懂啊。有范例给我看下吗?

mysql_connect();mysql_select_db('test');mysql_query('DROP TABLE IF EXISTS lookup');mysql_query('CREATE TABLE lookup (id INT, INDEX USING BTREE (id)) ENGINE = MEMORY');$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);foreach($a as $v) mysql_query("insert into lookup values ($v)");for($n=2; $n<5; $n++) {  echo "<h5>$n</h5>";  $rs = mysql_query("select a.id, group_concat(b.id) as grouping, count(*) as cnt from lookup a, lookup b where a.id<=b.id and a.id>b.id-$n group by a.id HAVING cnt=$n") or die(mysql_error());  while($row = mysql_fetch_assoc($rs)) {    echo $row['grouping'], '<br>';  }}
Copy after login

function LinkNum($array,$num){  if(count($array) < $num)  {      return 0;  }  $cishu = 0; //临时变量  $total = 0;  //N连号次数  for($i=0;$i<count($array);$i++)  {      if($array[$i] == $array[$i+1]-1)      {          $cishu++;          if($cishu == $num)          {              $i = $i+1;              $cishu = 0;          }      }      else      {          if($cishu == $num -1 && ($i - $num <= 0 || $array[$i] - $num != $array[$i - $num])) {              $total++;          }          $cishu = 0;      }  }  return $total;}echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有2连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),2).'组'.'<br>';echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有3连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),3).'组'.'<br>';echo "1,3,5,6,7,8,9,10,14,15,18,100,101".'有4连号'.LinkNum(array(1,3,5,6,7,8,9,10,14,15,18,100,101),4).'组'.'<br>';
Copy after login

1,3,5,6,7,8,9,10,14,15,18,100,101有2连号2组
1,3,5,6,7,8,9,10,14,15,18,100,101有3连号0组
1,3,5,6,7,8,9,10,14,15,18,100,101有4连号0组

我也写出来了,不过效率不咋地,遍历几十万次数组要好几分钟!
可能我没说清楚,这才是我想要的,2连号只能出现两个相邻的,其他不算;
就看有无高手可以优化下,我用笨方法这样试出来的!优化的朋友贴上代码我会追加分数

1,3,5,6,7,8,9,10,14,15,18,100,101有2连号2组 ?
1,3, 5,6, 7,8, 9,10, 14,15,18, 100,101有2连号2组
不是 5 组吗?

还没有算 8,9

1,3,5,6,7,8,9,10,14,15,18,100,101有2连号2组 ?
1,3, 5,6, 7,8, 9,10, 14,15,18, 100,101有2连号2组
不是 5 组吗?


5,6,7,8,9,10是6连号,不算2连,只有14,15,与100,101算2连

嗯,不计及连续数子集

$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);print_r(consecutive_numbers($a));echo consecutive_numbers($a, 4);function consecutive_numbers($ar,$num=0) {  $res = array();  for($i=0; $i<count($ar)-1; $i++) {    for($j=$i+1; isset($ar[$j]) && $ar[$j]-$ar[$i] == $j-$i; $j++);    if($j-$i > 1) {      $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i));      $i = --$j;    }  }  if($num) return count(@$ret[$num]);  return $res;}
Copy after login
Copy after login
Array(    [6] => Array        (            [0] => 5,6,7,8,9,10        )    [2] => Array        (            [0] => 14,15            [1] => 100,101        ))0
Copy after login
Copy after login

嗯,不计及连续数子集

$a = array(1,3,5,6,7,8,9,10,14,15,18,100,101);print_r(consecutive_numbers($a));echo consecutive_numbers($a, 4);function consecutive_numbers($ar,$num=0) {  $res = array();  for($i=0; $i<count($ar)-1; $i++) {    for($j=$i+1; isset($ar[$j]) && $ar[$j]-$ar[$i] == $j-$i; $j++);    if($j-$i > 1) {      $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i));      $i = --$j;    }  }  if($num) return count(@$ret[$num]);  return $res;}
Copy after login
Copy after login
Array(    [6] => Array        (            [0] => 5,6,7,8,9,10        )    [2] => Array        (            [0] => 14,15            [1] => 100,101        ))0
Copy after login
Copy after login


最后那个@$ret 是什么意思,好像没定义这个变量,是不是写错了

$ret 是数组,前面已经有定义($ret = array(); )
直接 $ret[$num] 可能引发 E_NOTICE 级别错误($ret[$num] 可能不存在,比如 $ret[4])
加个 @ 就可屏蔽掉错误信息

或者你改作  return count(isst($ret[$num] ? $ret[$num] : array());

$ret 是数组,前面已经有定义($ret = array(); )
直接 $ret[$num] 可能引发 E_NOTICE 级别错误($ret[$num] 可能不存在,比如 $ret[4])
加个 @ 就可屏蔽掉错误信息

或者你改作  return count(isst($ret[$num] ? $ret[$num] : array());


$res = array(); 定义的是这个吧!我拷贝到IDE里,查找都没找到
怎么最后就变成$ret了,第一感觉肯定是写错了

你那是什么 IDE?

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template