java - Finding the highest value of a varying number -


how find value of 'high score' of brackets?

private static boolean basicsweep(string input) {     int noofclosingparentheses = 0;     int noofopeningparentheses = 0;     int highscore = 0;     (int = 0; < input.length(); i++) {         character currentcharacter = input.charat(i);         if (currentcharacter == '(') {             noofopeningparentheses++;             highscore++;         }         else if (currentcharacter == ')') {             noofclosingparentheses++;         }     }     return false; } 

let's have string "((p)) & (q v (r & s))". 'high score', or maximum in case 2, tied between ((p)) , (...(r&s)). how go doing this? suspect store value in placeholder variable, i'm not sure variable go. current 'highscore' variable equal total number of opening parentheses, that's no good.

any appreciated. apologies vagueness - quite difficult explain!

note: method work in progress - no need commentary regarding lack of processing!

edit: attempted answer suggests set depth , maxdepth variables. unfortunately, doesn't work either, under following implementation:

int depth = 0;         int maxdepth = 0;         (int = 0; < 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 string "(((p))) & (p v (q <-> r))", whereas actual answer 3: (((p))).

try code

private static boolean basicsweep(string input) { int noofclosingparentheses = 0; int noofopeningparentheses = 0; int highscore = 0; (int = 0; < input.length(); i++) {     character currentcharacter = input.charat(i);     if (currentcharacter == '(') {         noofopeningparentheses++;      }     else if (currentcharacter == ')') {         noofclosingparentheses++;          if(noofopeningparentheses >= highscore) {           highscore = noofopeningparentheses;           }         noofopeningparentheses--;      } } return false; } 

let me know if looking for.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -