package com.mianshi.ms; import java.util.Stack; /** * 类说明 输入N对括号,输出正确的配对组合 */ public class NPair { public static Stack<String> doCompute(int N){ Stack<String > stack=new Stack<String>(); if(N==1){ stack.push("()"); return stack; } Stack<String> stack2=doCompute(N-1); while(stack2.size()>0){ String valString=stack2.pop(); stack.push("("+valString+")"); stack.push("()"+valString); if(!("()"+valString).equals(valString+"()")){ stack.push(valString+"()"); } } return stack; } public static void main(String[] args) { Stack<String> stack=new Stack<String>(); int N=3; stack=doCompute(N); for(String val:stack){ System.out.println(val); } } }