1. <?php
    2. class Solution {
    3. public $list = [];
    4. public function generateParenthesis($n) {
    5. $this->_gen(0, 0, $n, '');
    6. return $this->list;
    7. }
    8. private function _gen($left, $right, $num, $result) {
    9. echo $left . ' ' . $right . "\t";
    10. if ($left == $num && $right == $num) {
    11. array_push($this->list, $result);
    12. return;
    13. }
    14. if ($left < $num) {
    15. $this->_gen($left + 1, $right, $num, $result . '(');
    16. }
    17. if ($right < $num && $left > $right) {
    18. $this->_gen($left, $right + 1, $num, $result . ')');
    19. }
    20. return;
    21. }
    22. }
    23. $cls = new Solution();
    24. $r = $cls->generateParenthesis(3);
    25. print_r($r);