<?php
/**
* 举例:
* ()
* (()) ()()
* (())() ((())) ()()() (())() ()(())
*/
function combination($n){
    $char = '()';
    $result = array('');
    $k=0;
    for($i=1; $i<=$n; $i++){
        $result = insert($result, $char);
    }
    return $result;
}
function insert($arr, $char){
    $result = array();
    foreach($arr as $val){
        for($i=0; $i<strlen($val)+1; $i++){
            $str = substr($val,0,$i).$char.substr($val,$i,strlen($val)-$i);
            if(!in_array($str, $result)){
                $result[] = $str;
            }
        }
    }
    return $result;
}
print_r(combination($_GET['n']));
//end
转载请注明:小Y » 卡塔兰数计算n对小括号的有几种呢
php实现时间轮算法