2012-04-08 113 views
3

如果你能在Java中幫助我,我將不勝感激。圍繞圓括號標識符Java

給定兩個字符串,可以說String A = "(A+B)+(C)"String B = "((A+B)+(C))"String C = (A+B)String D = A+(B+C)String E = (A+(B+C))

我怎麼能確定字符串是否是用括號如String B.

在例如完全包圍:boolean flag(String expr) { //return false if surrounded, else true }

如果expr = A,標誌將返回true

如果expr = B,標誌將返回false

如果expr = C,標誌將返回false

如果expr = D,標誌將返回true

如果expr = E,標誌將返回FLASE

很抱歉,如果它是不明確,但它應該適用於任何字符串表達式:

假設表達式僅包含Digits運營商括號

謝謝。欣賞它。

+5

是它的功課? – Jack 2012-04-08 19:03:59

+0

(A + B)被包圍,所以它應該返回false – 2012-04-08 19:19:58

+0

哦對不起,我得到了真正的和錯誤的回到前面:你想檢測字符串何時* NOT *完全被括號包圍。我會更新我的答案。 – 2012-04-08 19:21:44

回答

4

你不能用正則表達式來做它,因爲嵌套的括號不是常規的語言。

而是迭代字符串並通過計算開合圓括號的數量來跟蹤嵌套層次。爲每個左括號添加一個嵌套級別。對於每個右括號減1。

  • 如果在到達字符串末尾之前達到零(或更少),則返回true。
  • 如果您在最後達到零,則返回false。
  • 其他任何東西都是不平衡的圓括號,除非您的輸入無效,否則不應該發生。

這裏有一些工作的例子來證明一個道理:

(A+B)+(C) 
11110  TRUE 

((A+B)+(C)) 
12222112210 FALSE 

(A+B) 
11110  FALSE 

A+(B+C) 
0   TRUE 

(A+(B+C)) 
111222210 FALSE 

*三立

+0

謝謝,我會試試 – 2012-04-08 19:16:46

+0

「A +(B + C)」表達式的返回值如何?它是一個常規用例嗎? – 2012-04-08 19:50:24

+0

是的,那個應該返回true,因爲它沒有被包圍 – 2012-04-08 19:52:09

1

我看到你的情況2個選項。

  1. 使用子方法

實施例:

public boolean checkForParanthesis(String str) { 
Integer last = str.length() - 1; // Get number of the last character 
String firstChar = str.substring(0); // Get first character of the string 
String lastChar = str.substring(last); // Get last character of the string 
if (firstChar.equals("(") && lastChar.equals(")")) return false; 
return true 
} 
  1. 使用reqular表達。也許這是一個更好的解決方案。
+1

以上三種情況都會返回false – 2012-04-08 19:23:51

1

馬克·拜爾斯的算法似乎大致是你在找什麼。現在把它放在一起,你必須使用forif Java關鍵字和索引。一個例子可以是下面的代碼。但是,它不驗證表達式,因此,例如,當沒有錯誤發生時測試了A+B)無效表達式(僅返回true值)。檢查它並自己測試。希望這有助於有點...代碼

 

package test; 

public class Main { 

    public static void main(String[] args) { 
    Main m = new Main(); 
    m.start(); 
    } 

    private void start() { 
    /* true */ 
    System.out.println(isNotSurrounded("A")); 
    System.out.println(isNotSurrounded("A+B")); 
    System.out.println(isNotSurrounded("A+(B+C)")); 
    System.out.println(isNotSurrounded("(B+C)+D")); 
    System.out.println(isNotSurrounded("A+(B+C)+D")); 
    System.out.println(isNotSurrounded("(A+B)+(C)")); 
    System.out.println(isNotSurrounded("(A)+(B)+(C)")); 
    System.out.println(isNotSurrounded("(A)+((B)+(C))+(D+E+F+(G))")); 
    /* false */ 
    System.out.println(); 
    System.out.println(isNotSurrounded("(A)")); 
    System.out.println(isNotSurrounded("(A+B)")); 
    System.out.println(isNotSurrounded("(A+(B+C))")); 
    System.out.println(isNotSurrounded("((B+C)+D)")); 
    System.out.println(isNotSurrounded("(A+(B+C)+D)")); 
    System.out.println(isNotSurrounded("((A+B)+(C))")); 
    System.out.println(isNotSurrounded("((A)+(B)+(C))")); 
    System.out.println(isNotSurrounded("((A)+((B)+(C))+(D+E+F+(G)))")); 
    } 

    private boolean isNotSurrounded(String expression) { 
    if (expression.startsWith("(") && expression.endsWith(")") && expression.length() > 2) { 
     int p = 0; 
     for (int i = 1; i < expression.length() - 1; i++) { 
     if (expression.charAt(i) == '(') { 
      p++; 
     } else if (expression.charAt(i) == ')') { 
      p--; 
     } 
     if (p < 0) { 
      return true; 
     } 
     } 
     if (p == 0) { 
     return false; 
     } 
    } 
    return true; 
    } 
} 
 

輸出如下:

 

true 
true 
true 
true 
true 
true 
true 
true 

false 
false 
false 
false 
false 
false 
false 
false