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

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

WBOY
WBOYOriginal
2016-06-13 11:55:03834browse

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

求解
------解决方案--------------------

$a = array(1,2,2,3,1,2,3,1,1);<br />$st = array(1 => 0);<br />$d = 1;<br />foreach($a as $v) {<br />  @$st[$v]++;<br />  if($v >= $d) {<br />    $d = $v;<br />  }else{<br />    while($v < $d) {<br />      unset($st[$d--]);<br />    }<br />  }<br />  $r[] = join('.', $st);<br />}<br />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 表示当前的层次

Statement:
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
Previous article:sql 话语不执行Next article:felayman-PHP环境搭建