Finding all combinations of well-formed brackets

后端 未结 29 1647
盖世英雄少女心
盖世英雄少女心 2020-11-28 02:34

This came up while talking to a friend and I thought I\'d ask here since it\'s an interesting problem and would like to see other people\'s solutions.

The task is to

29条回答
  •  时光说笑
    2020-11-28 02:52

    ruby version:

    def foo output, open, close, pairs
      if open == pairs and close == pairs
          p output
      else
        foo(output + '(', open+1, close, pairs) if open < pairs
        foo(output + ')', open, close+1, pairs) if close < open
      end
    end
    foo('', 0, 0, 3)
    

提交回复
热议问题