找到不同数字的最高值

时间:2015-05-29 17:28:45

标签: 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 = input.charAt(i);
        if (currentCharacter == '(') {
            noOfOpeningParentheses++;
            highScore++;
        }
        else if (currentCharacter == ')') {
            noOfClosingParentheses++;
        }
    }
    return false;
}

假设我们有字符串“((P))&amp;(Q v(R&amp; S))”。 “高分”或在这种情况下的最大值将是2,在((P))和(......(R&amp; S))之间。我该怎么做呢?我怀疑你将值存储在占位符变量中,但我不确定这个变量到底在哪里。当前的'highScore'变量仅等于开括号的总数,所以这没有用。

任何帮助非常感谢。对任何含糊不清的道歉 - 这很难解释!

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

编辑:尝试回答建议设置深度和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)))&amp;(PV(Q&lt; - &gt; R))”,而实际答案为3:(((P)))。< / p>

3 个答案:

答案 0 :(得分:2)

试试这段代码

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;
}

如果您正在寻找,请告诉我。

答案 1 :(得分:0)

首先设置depth = 0和maxDepth = 0

每次扫描仪看到&#39;(&#39;,每次减少深度&#39;)时,扫描字符串,增加深度。看到了。

设置maxDepth =深度,只要增加深度使其变得大于maxDepth。

答案 2 :(得分:0)

我会使用堆栈在另一个方向上进行此操作。这是基于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;
}