Get the number connection question in the array $ result = array (
'1, 3, 5, 6, 8, 9, 10, 14, 15, 18, 100,101 ',
'1, 100,101,102'
);
Evaluate a function to calculate the type and number of times the number element contains a hyphen
For example, the first group: Group 1, Group 6, and group 2
Group 1: Group 6, group 1, Group 2, Group 3, Group 1
My current program can only achieve 1, 2, 5, 6, 7, 8, 9, 10, think this is 2 connection 6 group! Think for a long time
Reply to discussion (solution)
Put it in the database and use self-connection for ease
The statements are provided upstairs.
Put it in the database and use self-connection for ease
No. Can you show me an example?
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 "$n"; $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'], '
'; }}
Function LinkNum ($ array, $ num) {if (count ($ array) <$ num) {return 0 ;}$ cishu = 0; // temporary variable $ total = 0; // N consecutive times for ($ I = 0; $ I
'; Echo "18,100,101 ". there are 3 connection numbers '. linkNum (array (18,100,101,), 3 ). 'Group '.'
'; Echo "18,100,101 ". 'There are 4 serial numbers '. linkNum (array (18,100,101,), 4 ). 'Group '.'
';
1, 3, 5, 6, 7, 8, 9, 10, 18,100,101, have 2 connection numbers and 2 groups
Group 1, 3, 5, 6, 7, 8, 9, 10, 18,100,101, 3, and 0
Group 1, 3, 5, 6, 7, 8, 9, 10, 18,100,101, 4, and 0
I also wrote it out, but the efficiency is not bad. it takes several minutes to traverse hundreds of thousands of times!
Maybe I didn't make it clear. this is what I want. a 2-line connection number can only contain two adjacent ones. Others are not counted;
Let's see if there are any experts to optimize it. I tried it in this stupid way! I will append scores to optimized friends by posting code.
1, 3, 5, 6, 7, 8, 9, 10, 14, 15, 18,100,101 have 2 connection numbers and 2 groups?
1, 3, 5, 6,7, 8, 9, 10,14,15, 18,100,101 has 2 connection numbers and 2 groups
Aren't there five groups?
Not 8, 9
1, 3, 5, 6, 7, 8, 9, 10, 14, 15, 18,100,101 have 2 connection numbers and 2 groups?
1, 3, 5, 6,7, 8, 9, 10,14,15, 18,100,101 has 2 connection numbers and 2 groups
Aren't there five groups?
5, 6, 7, 8, 9, 10 is a 6-line connection, not counted as 2, only 100,101, and are counted as 2
Well, excluding and consecutive data subsets
$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
1) { $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i)); $i = --$j; } } if($num) return count(@$ret[$num]); return $res;}
Array( [6] => Array ( [0] => 5,6,7,8,9,10 ) [2] => Array ( [0] => 14,15 [1] => 100,101 ))0
Well, excluding and consecutive data subsets
$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
1) { $res[$j-$i][] = join(",", array_slice($ar, $i, $j-$i)); $i = --$j; } } if($num) return count(@$ret[$num]); return $res;}
Array( [6] => Array ( [0] => 5,6,7,8,9,10 ) [2] => Array ( [0] => 14,15 [1] => 100,101 ))0
What does the last @ $ ret mean? it seems that this variable is not defined. Is it wrong?
$ Ret is an array, which is defined previously ($ ret = array ();)
Directly $ ret [$ num] may cause an E_NOTICE-level error ($ ret [$ num] may not exist, for example, $ ret [4])
Add @ to block the error message.
Or you can change it to return count (isst ($ ret [$ num]? $ Ret [$ num]: array ());
$ Ret is an array, which is defined previously ($ ret = array ();)
Directly $ ret [$ num] may cause an E_NOTICE-level error ($ ret [$ num] may not exist, for example, $ ret [4])
Add @ to block the error message.
Or you can change it to return count (isst ($ ret [$ num]? $ Ret [$ num]: array ());
$ Res = array (); defines this! I copied it to IDE, but I couldn't find it.
How can it be changed to $ ret at last? the first thought must be wrong.
What is your IDE?