【问题标题】:Finding the highest value of a varying number寻找变化数的最大值
【发布时间】:2015-05-29 17:28:44
【问题描述】:

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

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))”。 “高分”,或在这种情况下的最大值将是 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);

使用字符串“(((P))) & (P V (Q R))”时,maxDepth 将为 2,而实际答案为 3:(((P)))。

【问题讨论】:

  • 根据您的输入,高分将返回 4 而不是 2
  • 好力,我说清楚了:“当前的‘highScore’变量只等于左括号的总数……”。

标签: java variables for-loop updates


【解决方案1】:

试试这个代码

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

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

【讨论】:

  • 非常感谢!这种方法似乎是一种稳健的方法:)
【解决方案2】:

首先设置 depth=0 和 maxDepth=0

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

每当增加深度使其大于 maxDepth 时,设置 maxDepth=depth。

【讨论】:

    【解决方案3】:

    我会使用堆栈从另一个方向着手。这是基于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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多