寻找不同数字的最大值

Finding the highest value of a varying number

如何找到 'high score' 个括号的值?

private static boolean basicSweep(String input) {
    int noOfClosingParentheses = 0;
    int noOfOpeningParentheses = 0;
    int highScore = 0;
    for (int i = 0; i < input.length(); i++) {
        Character currentCharacter = input.charAt(i);
        if (currentCharacter == '(') {
            noOfOpeningParentheses++;
            highScore++;
        }
        else if (currentCharacter == ')') {
            noOfClosingParentheses++;
        }
    }
    return false;
}

假设我们有字符串“((P)) & (Q v (R & S))”。 'high score' 或本例中的最大值为 2,位于 ((P)) 和 (...(R&S)) 之间。我该怎么做呢?我怀疑您将该值存储在占位符变量中,但我不确定该变量的确切位置。当前的'highScore'变量只等于左括号的总数,这样不行。

非常感谢任何帮助。对任何含糊之处表示歉意 - 这很难解释!

注意:该方法正在进行中 - 无需对缺少处理进行任何评论!

编辑:尝试回答建议设置 depth 和 maxDepth 变量。不幸的是,这在以下实现下也不起作用:

int depth = 0;
        int maxDepth = 0;
        for (int i = 0; i < input.length(); i++) {
            Character currentCharacter = input.charAt(i);
            if (currentCharacter == '(') {
                noOfOpeningParentheses++;
                depth++;
                maxDepth = depth;
            }
            else if (currentCharacter == ')') {
                noOfClosingParentheses++;
                depth--;
            }
        }
        System.out.println(maxDepth);

maxDepth 将是 2 和字符串“(((P))) & (P V (Q <-> R))”,而实际答案是 3:(((P))).

首先设置 depth=0 和 maxDepth=0

扫描字符串,每次扫描器看到“(”时增加深度,每次看到“)”时减少深度。

每当增加深度导致它变得大于 maxDepth 时,设置 maxDepth=depth。

试试这个代码

private static boolean basicSweep(String input) {
int noOfClosingParentheses = 0;
int noOfOpeningParentheses = 0;
int highScore = 0;
for (int i = 0; i < input.length(); i++) {
    Character currentCharacter = input.charAt(i);
    if (currentCharacter == '(') {
        noOfOpeningParentheses++;

    }
    else if (currentCharacter == ')') {
        noOfClosingParentheses++;
         if(noOfOpeningParentheses >= highScore) {
          highScore = noOfOpeningParentheses;
          } 

      noOfOpeningParentheses--;

    }
}
return false;
}

如果这是您要找的,请告诉我。

我会使用堆栈从另一个方向着手解决这个问题。这是基于 http://en.wikipedia.org/wiki/Shunting-yard_algorithm#Detailed_example 的简化版本。但是我只用了关于括号的部分

private static boolean basicSweep(String input) {
    Stack<String> stack = new Stack<>();
    int value = 0;
    int noOfClosingParentheses = 0;
    int noOfOpeningParentheses = 0;
    int highScore = 0;
    for (int i = 0; i < input.length(); i++) {
        Character currentCharacter = input.charAt(i);
        if (currentCharacter == '(') {
            stack.push("(");//Put a open bracket on the stack
            noOfOpeningParentheses++;
        }
        else if (currentCharacter == ')') {
            while(!stack.isEmpty()){ //
                stack.pop(); //Pop openingparentheses from the stack until none are left 
                value++;//Counting the number of opening parentheses
            }
            highScore = Math.max(highScore, value); //Get the maximum value of our highscore and our maximal value we have found
            value= 0; //Reset counter
            noOfClosingParentheses++;
        }
    }
    return false;
}