Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

关于数组排序问题,相同值不能连续出现三次。

如题,重排后的数组,相同值不能连续出现三次。

$arr = array(1,1,1,2,2,2,3,3,3,3);

在线等!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

计数器的方式:

<?php
$arr = array(1,1,1,2,2,2,3,3,3,3,4,5,6,6,6);

function fsort($ss){
    $result = array($ss[0]);
    $overage = array();
    $num = 1;
    for($i=1, $len=count($ss); $i<$len; $i++){
        if( $ss[$i]==$ss[$i-1] ){
            if( $num<2 ){
                $result[] = $ss[$i];
                $num++;
            }else{
                $overage[] = $ss[$i];
            }
        }else{
            $result[] = $ss[$i];
            $num=1;
        }
    }
    return array_merge($result, $overage);
}


echo "<pre>";
print_r( fsort($arr) );

如果你能把问题说清楚,就没这么麻烦了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...