Algorithm to print all valid combations of n pairs of parenthesis
问题 I'm working on the problem stated in the question statement. I know my solution is correct (ran the program) but I'm curious as to whether or not I'm analyzing my code (below) correctly. def parens(num) return ["()"] if num == 1 paren_arr = [] parens(num-1).each do |paren| paren_arr << paren + "()" unless "()#{paren}" == "#{paren}()" paren_arr << "()#{paren}" paren_arr << "(#{paren})" end paren_arr end parens(3), as an example, will output the following: ["()()()", "(()())", "(())()", "()(())