合法括号

题目一:

  输入括号对数,输出所有的合法组合,比如输入1,输出"()",输入3,输出"()()(), (()()), ()(()), (())(), ((()))"。

  思路:比如输入1,输出"()",那么输入2的话,我们就可以在输入1的基础上往左插入一对括号,往右插入一对括号以及把它用括号包含起来。这样的话左边和右边是相同的,我们可以用set集合来去除重复。画个示意图如下:

    

  代码:

 1 import java.util.HashSet;
 2 import java.util.Set;
 3 
 4 public class 合法括号 {
 5 
 6     public static void main(String[] args) {
 7         Set<String> parenthesis = parenthesis(3);
 8         System.out.println(parenthesis);
 9         System.out.println("==============================");
10         parenthesis = parenthesis1(3);
11         System.out.println(parenthesis);
12     }
13     
14     /* 逐步生成之递归解法 */
15     public static Set<String> parenthesis(int n) {
16         Set<String> s_n = new HashSet<>();
17         if (n == 1) {
18             s_n.add("()");
19             return s_n;
20         }
21         Set<String> s_n_1 = parenthesis(n - 1);
22         for (String eOfN_1 : s_n_1) {
23             s_n.add("()" + eOfN_1);
24             s_n.add(eOfN_1 + "()");
25             s_n.add("(" + eOfN_1 + ")");
26         }
27         return s_n;
28     }
29 
30     /* 迭代形式 */
31     public static Set<String> parenthesis1(int n) {
32         Set<String> res = new HashSet<>(); // 保存上次迭代的状态
33         res.add("()");
34         if (n == 1) {
35             return res;
36         }
37         for (int i = 2; i <= n; i++) {
38             Set<String> res_new = new HashSet<>();
39 
40             for (String e : res) {
41                 res_new.add(e + "()");
42                 res_new.add("()" + e);
43                 res_new.add("(" + e + ")");
44             }
45             res = res_new;
46         }
47         return res;
48     }
49 
50 }

  结果:

    

  注意:这儿的递归思路有误,会漏算。当n=4时,"(())(())"这种情况就打印不出来,不过上面的代码也算是给我们提供了一种递归的思路。下面写出正确的递归思路:   

  解题思路:括号对的合法序列,已经插入的左括号的数目大于等于右括号的数目。
   (1)插入左括号:剩余的括号中,还有左括号;
   (2)插入右括号:剩余的括号中,右括号的数目大于左括号的数目;

  代码及其结果:

 1 public class 合法括号_1 {
 2     public static void printPar(int l, int r, char[] str, int count) {
 3         if (l < 0 || r < l)
 4             return;
 5         if (l == 0 && r == 0) {
 6             System.out.println(str);
 7         } else {
 8             if (l > 0) {
 9                 str[count] = '(';
10                 printPar(l - 1, r, str, count + 1);
11             }
12             if (r > l) {
13                 str[count] = ')';
14                 printPar(l, r - 1, str, count + 1);
15             }
16         }
17 
18     }
19 
20     public static void main(String[] args) {
21         int count = 4;
22         char str[] = new char[count * 2];
23         printPar(count, count, str, 0);
24     }
25 }
26 
27 //结果
28 /*
29 (((())))
30 ((()()))
31 ((())())
32 ((()))()
33 (()(()))
34 (()()())
35 (()())()
36 (())(())
37 (())()()
38 ()((()))
39 ()(()())
40 ()(())()
41 ()()(())
42 ()()()()
43 */

题目二:

  判断一个括号字符串是否合法。

  思路:直接看代码即可,很容易懂,这里要注意一下中文括号和英文括号是不同的。

  代码:

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         System.out.println(chkParenthsis("()a()", 5));
 5         System.out.println(chkParenthsis("()()", 4));
 6         System.out.println(chkParenthsis(")())", 4));
 7         System.out.println(chkParenthsis("(())", 4));
 8         System.out.println("测试中文括号:"+chkParenthsis("()()", 4));
 9     }
10     
11     /**
12      * 检查括号字符串是否合法
13      * @param A 源串
14      * @param n 源串长度
15      * @return
16      */
17     public static boolean chkParenthsis(String A,int n){
18         if(n%2!=0){
19             return  false;
20         }
21         int count = 0;
22         for (int i = 0; i < A.length(); i++) {
23             if (A.charAt(i) == '('||A.charAt(i) == '(') {  // 避免中文括号
24                 count++;
25             } else if (A.charAt(i) == ')'||A.charAt(i) == ')') {
26                 count--;
27             } else
28                 return false;
29             if (count < 0)
30                 return false;
31         }
32         return true;
33     }
34 }

   结果:

    

原文地址:https://www.cnblogs.com/xiaoyh/p/10341783.html