[英]Print all valid combinations of n-pairs of parentheses
左:只要我们没有用完所有左括号,就可以随时插入左括号。 正确:只要不引起语法错误,我们就可以插入正确的括号。 什么时候会出现语法错误
public class parentheses {
public static void printPar(int l, int r, char[] str, int count){ //Use recursion method to
// print the parentheses
if(l == 0 && r == 0){ //if there are no parentheses available, print them out
System.out.println(str); //Print out the parentheses
}
else{
if(l > 0){ // try a left paren, if there are some available
str[count] = '(';
printPar(l - 1, r, str, count + 1); //Recursion
}
if(r > 0){ // try a right paren, if there are some available
str[count] = ')';
printPar(l, r - 1, str, count + 1); //Recursion
}
}
}
public static void printPar(int count){
char[] str = new char[count*2]; // Create a char array to store the parentheses
printPar(count,count,str,0); //call the printPar method, the parameters are the left,
//the right parentheses, the array to store the
//parenthese, and the counter
}
public static void main(String[] args) {
// TODO Auto-generated method stub
printPar(2); //
}
}
结果应为:
(())
()()
但是我得到的是:
(())
()()
())(
)(()
)()(
))((
import java.util.ArrayList;
import java.util.List;
public class parentheses {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(prent(2));
}
public static List<String> prent(int n) {
List<String> l = new ArrayList<String>();
if(n==1) {
l.add("()");
return l;
}
for(String st : prent(n-1)) {
l.add(st+"()");
l.add("("+st+")");
if(!(st+"()").equals("()"+st)) {
l.add("()"+st);
}
}
return l;
}
}
试试这个代码。 经过测试,工作正常。
public class ParanthesisCombination {
public static void main(String[] args) {
printParenthesis(3);
}
static void printParenthesis(int n){
printParenthesis("",n,n);
}
static void printParenthesis(String s,int open,int close){
if(open>close)
return;
if(open == 0 && close == 0){
System.out.println(s);
return;
}
if(open < 0 || close<0)
return;
printParenthesis(s + '{',open-1,close);
printParenthesis(s + '}',open,close-1);
}
}
private static void printA(int open, int close, int max, String out) {
if(open==close && close==max){
st.add(out);
System.out.println(out);
} else {
if(open+1<=max){
printA(open+1, close, max, out+"(");
}
if(open>close && close+1<=max){
printA(open, close+1, max, out+")");
}
}
}
public static ArrayList<String>st = new ArrayList<String>();
public static void main(String[] args) {
//2 is maximum open/close parenthese
//i save the output in st(arraylist),
printA(0,0,2,"");
}
我认为问题在于代码中没有任何部分执行“ no sintax error ”部分。
您应该将if(r > 0){...}
if(r > l){...}
更改为if(r > 0){...}
if(r > l){...}
以执行该规则; 如果至少有1个左括号仍未“打开”,则不应打印右括号。
让我们假设给出的总对数为3-要保持有效,您只需要在开始时打开大括号,在结束时一个关闭大括号,这意味着(2对组合)。
因此2个对中的总有效+无效组合= 3 * 2 =6。这些没有重复。.认为'('为0,')'为1。
0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111
只能有6个可能的组合具有2 0和2 1。.因此,对于2对总组合= 6因此对于3对总有效组合= 6
public class parentheses {
public static void printPar(int l, int r, char[] str, int count){ //Use recursion method to
// print the parentheses
if(l == 0 && r == 0){ //if there are no parentheses available, print them out
System.out.println(str); //Print out the parentheses
}
else{
if(l > 0){ // try a left paren, if there are some available
str[count] = '(';
printPar(l - 1, r, str, count + 1); //Recursion
}
// Add constraint that a parenthesis is open before inserting a closing paranthesis
// hence, add l < r - meaning one 'l' is printed before going for an 'r'
if(r > 0 && l < r){ // try a right paren, if there are some available
str[count] = ')';
printPar(l, r - 1, str, count + 1); //Recursion
}
}
}
public static void printPar(int count){
char[] str = new char[count*2]; // Create a char array to store the parentheses
printPar(count,count,str,0); //call the printPar method, the parameters are the left,
//the right parentheses, the array to store the
//parenthese, and the counter
}
public static void main(String[] args) {
printPar(2); //
}
}
public class parentheses {
public static void printPar(int l, int r, char[] str, int count){
if(i<0 || r<l) return;
...
}
}
您只失去了结束条件。 希望这会有所帮助。
public class ValidParenthesisCombi {
public static void main(String[] args) {
final int noOfBraces = 3;
print(noOfBraces);
}
public static void print(int n){
print("",n,n);
}
public static void print( String s, int open, int close){
if(open > close){
return;
}
if((open==0)&&(close==0)){
System.out.println(s);
}else{
if(open > 0){
print(s+"{",open-1, close);
}if(close > 0){
print(s+"}",open, close-1);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.