📜  生成括号 - 无论代码示例

📅  最后修改于: 2022-03-11 14:55:50.004000             🧑  作者: Mango

代码示例1
import java.util.HashSet;
     import java.util.Set;

     public class BalancedParanthesis {
       public static void main(String args[]){
         int noOfBrackets = 3;
         HashSet hs=new HashSet(generate(noOfBrackets));
         System.out.println(hs);
         }
         public static HashSet generate(int in)
         {
          HashSet hs= new HashSet();
          if(in ==1)
         {
        hs.add("()");
        return hs;
         }
        else{
           Set ab=generate(in-1);
           for(String each:ab)
            {
            hs.add("("+each+")");
            hs.add("()"+each);
            hs.add(each+"()");
            }
        return hs;
         }
        }
       }