作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种方法来检查数字 2D PHP 数组的值是否在
递增 ,
降序 ,
或
混合 订单。
示例:
array( 1, 2, 3, 4 ) // This is an incereasing numeric array
array( 4, 3, 2, 1 ) // This is a descending numeric array
array( 1, 3, 2, 4 ) // This is a mixed numeric array
最佳答案
我认为,如果您正在寻找快速解决方案(即快速工作),您将不得不使用您的数据数组,例如:
function getOrder($rgData)
{
if(!count($rgData) || count($rgData)==1)
{
return null;
}
$sCurrent = (string)array_shift($rgData);
$iOrder = current($rgData)>$sCurrent?1:-1;
foreach($rgData as $mValue)
{
if(($sCurrent>(string)$mValue && $iOrder== 1) ||
($sCurrent<(string)$mValue && $iOrder==-1))
{
return 0;
}
$sCurrent = (string)$mValue;
}
return $iOrder;
}
O(N)
使用
sort()
时的复杂性(在最坏的情况下)函数将导致
O(N log(N))
复杂性(最好的情况)
关于php - 如何检查数字 2D PHP 数组的值是否为 'incereasing' 、 'descending' 或 'mixed' 顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18715028/
我正在寻找一种方法来检查数字 2D PHP 数组的值是否在 递增 , 降序 , 或 混合 订单。 示例: array( 1, 2, 3, 4 ) // This is an incereasing n
我是一名优秀的程序员,十分优秀!