In PHP development, arrays are one of the most important data structures. Among them, finding the consecutive numbers of an array is a very common requirement. This article will describe different ways to do this.
The loop traversal method is a common method to find the consecutive numbers in an array. The basic idea is: assuming the first element in the array is the starting point, traverse the elements backwards from this starting point, and calculate whether the difference between the next element and the current element is 1 each time. If yes, it means there are consecutive numbers in the array. If not, reset the starting point and continue traversing.
The sample code is as follows:
function findConsecutiveNumbers($arr){ $res = []; $n = count($arr); for($i=0;$i<$n;$i++){ $j=$i+1; $tmp=[]; $tmp[] = $arr[$i]; while($j<$n && $arr[$j]-$arr[$j-1]==1){ $tmp[] = $arr[$j]; $j++; } if(count($tmp)>1){ $res[] = $tmp; } } return $res; } $arr = [1, 2, 3, 5, 6, 7, 9]; $res = findConsecutiveNumbers($arr); print_r($res); //输出[[1,2,3],[5,6,7]]
The sort method is another common method for obtaining consecutive numbers in an array. The basic idea is: first sort the original array, then traverse the array elements in sequence, and calculate whether the difference between each element and the previous element is 1. If yes, it means there are consecutive numbers in the array. If not, reset the starting point and continue traversing.
The sample code is as follows:
function findConsecutiveNumbers($arr){ sort($arr); $res = []; $n = count($arr); $tmp = []; for($i=0;$i<$n;$i++){ if(!$i || $arr[$i]-$arr[$i-1]==1){ $tmp[] = $arr[$i]; }else{ if(count($tmp)>1){ $res[] = $tmp; } $tmp = [$arr[$i]]; } } if(count($tmp)>1){ $res[] = $tmp; } return $res; } $arr = [1, 2, 3, 5, 6, 7, 9]; $res = findConsecutiveNumbers($arr); print_r($res); //输出[[1,2,3],[5,6,7]]
The recursive method is also a method of obtaining the continuous number of an array, by traversing each element recursively , and determine whether the difference between each element and the previous element is 1. If the difference is 1, continue recursing to the next element. If the difference is not 1, it means the continuous number is over and the previous level of recursion is returned.
The sample code is as follows:
function findConsecutiveNumbers($arr) { $res = array(); $n = count($arr); $i = 1; $j = 0; while($i < $n) { if($arr[$i] - $arr[$i-1] == 1) { $i++; } else { $tmp = array_slice($arr, $j, $i-$j); if(count($tmp)>1){ $res[] = $tmp; } $j = $i; $i++; } } $tmp = array_slice($arr, $j, $i-$j); if(count($tmp)>1){ $res[] = $tmp; } return $res; } $arr = [1, 2, 3, 5, 6, 7, 9]; $res = findConsecutiveNumbers($arr); print_r($res); //输出[[1,2,3],[5,6,7]]
Summary:
Through the above three methods, we can easily find the consecutive numbers of the array. Among them, the time complexity of the loop traversal method and the sorting method is $O(nlogn)$, and the time complexity of the recursive method is $O(n)$. Therefore, when the amount of data is large, we can give priority to using the recursive method.
The above is the detailed content of How to find the consecutive numbers of an array in php. For more information, please follow other related articles on the PHP Chinese website!