【问题标题】:Surrounding Parenthesis Identifier Java括号标识符Java
【发布时间】:2012-04-21 08:57:21
【问题描述】:

如果您能在 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))

如何识别字符串是否完全被括号包围,如字符串 B。

例如:boolean flag(String expr) { //return false if surrounded, else true }

如果expr = A,flag 将返回 true

如果expr = B,flag 将返回 false

如果expr = C,flag 将返回 false

如果expr = D,flag 将返回 true

如果expr = E,flag会返回flase

对不起,如果不清楚,但它应该适用于任何字符串表达式:

假设表达式仅包含 DigitsOperators 以及 Parenthesis

谢谢。欣赏它。

【问题讨论】:

  • (A+B) 被包围,所以它应该返回 false
  • 哦,对了,对不起,我得到了正确和错误的结果:您想检测字符串何时 NOT 完全被括号包围。我会更新我的答案。

标签: java regex string char iteration


【解决方案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

*理智

【讨论】:

  • A+(B+C) 表达式的返回值怎么样?它是一个常规用例吗?
  • 是的,应该返回true,因为它没有被包围
  • 他可能意思是,但正如现在提出的问题,我们只对是否被包围的相当琐碎的陈述感兴趣(而不是括号是否被包围)匹配正确)。这适用于匹配 \(.*\) 或类似的东西。
  • @Jiri 不,他不是,他解决的问题与您认为应该解决的问题略有不同。这个问题措辞......次优。
  • 呜,我猜你错了,因为(A)+(B) 返回的值与(A+B) 不同。在您的解决方案中,它将始终返回相同的值。
【解决方案2】:

我看到你的情况有 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. 使用正则表达式。也许这是一个更好的解决方案。

【讨论】:

  • 以上所有 3 种情况都将返回 false
【解决方案3】:

Mark Byers 的算法似乎大致就是您正在寻找的。现在把它放在一起,你必须使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多