Finding all combinations of well-formed brackets

后端 未结 29 1605
盖世英雄少女心
盖世英雄少女心 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 03:10

    In C#

        public static void CombiParentheses(int open, int close, StringBuilder str)
        {
            if (open == 0 && close == 0)
            {
                Console.WriteLine(str.ToString());
            }
            if (open > 0) //when you open a new parentheses, then you have to close one parentheses to balance it out.
            {                
                CombiParentheses(open - 1, close + 1, str.Append("{"));
            }
            if (close > 0)
            {                
                CombiParentheses(open , close - 1, str.Append("}"));
            }
        }
    

提交回复
热议问题