[R ,112个 107 99字节
非递归方法。我们使用“ <”和“>”,因为它避免了正则表达式中的转义字符。为了使我们可以将较短的规范用于ASCII范围,我们使用expand.grid
(通过其ASCII代码60、61和62)生成3 ^ 2n个2n个字符字符串“ <”,“ =“和”>” ,然后使用grep看看哪些组合能使括号括起来。当然,“ =”可能性将被忽略。
通过http://rachbelaid.com/recursive-regular-experession/
function(n)sort(grep("^(<(?1)*>)(?1)*$",apply(expand.grid(rep(list(60:62),2*n)),1,intToUtf8),,T,T))
在线尝试!
说明
"^(<(?1)*>)(?1)*$" = regex for balanced <> with no other characters
^ # match a start of the string
( # start of expression 1
< # open <>
(?1)* # optional repeat of any number of expression 1 (recursive)
# allows match for parentheses like (()()())(()) where ?1 is (\\((?1)*\\))
> # close <>
) # end of expression 1
(?1)* # optional repeat of any number of expression 1
$ # end of string
function(n)
sort(
grep("^(<(?1)*>)(?1)*$", # search for regular expression matching open and close brackets
apply(
expand.grid(rep(list(60:62),2*n)) # generate 3^(2n) 60,61,62 combinations
,1,intToUtf8) # turn into all 3^(2n) combinations of "<","=",">"
,,T,T) # return the values that match the regex, so "=" gets ignored
) # sort them
R,107字节
通常的递归方法。
-1感谢@Giuseppe
f=function(n,x=0:1)`if`(n,sort(unique(unlist(Map(f,n-1,lapply(seq(x),append,x=x,v=0:1))))),intToUtf8(x+40))
在线尝试!