左:只要我们没有用尽所有左括号,我们总是可以插入左边的paren。 右图:只要不会导致语法错误,我们就可以插入一个右边的paren。我们什么时候会出现语法错误
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); //
}
}
结果应为:
(())
()()
但我得到的是:
(())
()()
())(
)(()
)()(
))((
答案 0 :(得分:4)
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;
}
}
答案 1 :(得分:1)
试试这段代码。测试和工作正常。
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);
}
}
答案 2 :(得分: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,"");
}
答案 3 :(得分:0)
我认为问题在于您的代码中没有任何部分强制执行“ no sintax error ”部分。
您应该更改if(r > 0){...}
if(r > l){...}
来强制执行该规则;如果没有至少1个左侧的肠衣仍然“打开”,则不应打印右括号。
答案 4 :(得分:0)
让我们假设总配对 - 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
答案 5 :(得分:0)
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); //
}
}
答案 6 :(得分:0)
public class parentheses {
public static void printPar(int l, int r, char[] str, int count){
if(i<0 || r<l) return;
...
}
}
你只是失去了结局条件。希望这可以提供帮助。
答案 7 :(得分:0)
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);
}
}
}
}