Home > Backend Development > PHP Tutorial > 刚刚遇到一个棘手的算法问题,求解

刚刚遇到一个棘手的算法问题,求解

WBOY
Release: 2016-06-23 13:58:45
Original
978 people have browsed it


求解


回复讨论(解决方案)

$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); 
Copy after login
Copy after login
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); 
Copy after login
Copy after login
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 表示当前的层次

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template