剛剛遇到一個棘手的演算法問題,求解

來源:互聯網
上載者:User
求解


回複討論(解決方案)

$a = array(1,2,2,3,1,2,3,1,1);$st = array(1 => 0);$d = 1;foreach($a as $v) {  @$st[$v]++;  if($v >= $d) {    $d = $v;  }else{    while($v < $d) {      unset($st[$d--]);    }  }  $r[] = join('.', $st);}print_r($r); 
Array
(
[0] => 1
[1] => 1.1
[2] => 1.2
[3] => 1.2.1
[4] => 2
[5] => 2.1
[6] => 2.1.1
[7] => 3
[8] => 4
)

不好意思,沒看明白是個啥意思。……。。…。…………。……。。…。……。。…。……。……。………

$a = array(1,2,2,3,1,2,3,1,1);$st = array(1 => 0);$d = 1;foreach($a as $v) {  @$st[$v]++;  if($v >= $d) {    $d = $v;  }else{    while($v < $d) {      unset($st[$d--]);    }  }  $r[] = join('.', $st);}print_r($r); 
Array
(
[0] => 1
[1] => 1.1
[2] => 1.2
[3] => 1.2.1
[4] => 2
[5] => 2.1
[6] => 2.1.1
[7] => 3
[8] => 4
)


版主,謝謝你的解答 ,如果可以的話,能不能講講你的思路呀,萬分感謝

不好意思,沒看明白是個啥意思。……。。…。…………。……。。…。……。。…。……。……。………


本題其實是一個目錄編排的問題,你可以看到優先順序決定著大小標題的顯示

$a = array(1,2,2,3,1,2,3,1,1); 表示的是節點所在的層次
$st = array(1 => 0); 儲存每層當前的序號
$d 表示當前的層次

嗯嗯 謝謝版主的解釋,我再好好研究研究,先把分送上

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.