合法括號

題目一:java

  輸入括號對數,輸出全部的合法組合,好比輸入1,輸出"()",輸入3,輸出"()()(), (()()), ()(()), (())(), ((()))"。測試

  思路:好比輸入1,輸出"()",那麼輸入2的話,咱們就能夠在輸入1的基礎上往左插入一對括號,往右插入一對括號以及把它用括號包含起來。這樣的話左邊和右邊是相同的,咱們能夠用set集合來去除重複。畫個示意圖以下:spa

    

  代碼:code

 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 }

  結果:blog

    

  注意:這兒的遞歸思路有誤,會漏算。當n=4時,"(())(())"這種狀況就打印不出來,不過上面的代碼也算是給咱們提供了一種遞歸的思路。下面寫出正確的遞歸思路:   遞歸

  解題思路:括號對的合法序列,已經插入的左括號的數目大於等於右括號的數目。
   (1)插入左括號:剩餘的括號中,還有左括號;
   (2)插入右括號:剩餘的括號中,右括號的數目大於左括號的數目;字符串

  代碼及其結果:class

 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 */

題目二:import

  判斷一個括號字符串是否合法。基礎

  思路:直接看代碼便可,很容易懂,這裏要注意一下中文括號和英文括號是不一樣的。

  代碼:

 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 }

   結果:

    

相關文章
相關標籤/搜索