Java 求变数的最大值

Java 求变数的最大值,java,variables,for-loop,updates,Java,Variables,For Loop,Updates,我如何找到括号“高分”的值 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 = inpu

我如何找到括号“高分”的值

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;
}
private静态布尔basicSweep(字符串输入){
int noofclosing括号=0;
int noofopening圆括号=0;
int高分=0;
对于(int i=0;i
假设我们有字符串“((p))&(qv(R&s))”。“高分”或本例中的最高分为2,介于((P))和(…(R&S))之间。我该怎么做呢?我怀疑您将该值存储在占位符变量中,但我不确定该变量的确切位置。当前的“highScore”变量仅等于左括号的总数,因此这是不好的

非常感谢您的帮助。对于任何含糊之处,我深表歉意——这很难解释

注意:该方法是一项正在进行的工作-不需要任何关于缺少处理的评论

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

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);
int-depth=0;
int maxDepth=0;
对于(int i=0;i

maxDepth将是2,字符串为“((p))&(pv(qr))”,而实际答案是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;
}
private静态布尔basicSweep(字符串输入){
int noofclosing括号=0;
int noofopening圆括号=0;
int高分=0;
对于(int i=0;i=高分){
高分=无括号;
} 
无开括号--;
}
}
返回false;
}

让我知道这是否是您要找的。

我将使用堆栈从另一个方向进行此操作。这是基于的简化版本。但我只使用了关于括号的部分

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;
}
private静态布尔basicSweep(字符串输入){
堆栈=新堆栈();
int值=0;
int noofclosing括号=0;
int noofopening圆括号=0;
int高分=0;
对于(int i=0;i
根据您的输入,high score将返回4而不是2Hurix,我明确表示:“当前的'highScore'变量仅等于左括号的总数…”。非常感谢!这种方法似乎很可靠:)