2012-04-08 44 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