这个While循环能简化吗?

huangapple go评论69阅读模式
英文:

Can this While loop be simplified?

问题

考虑以下代码。它用于检查字符串是否具有有效的括号,但不使用堆栈。

public boolean isValid(String input) {
       
    while(input.length() != (input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "")).length());
    return input.isEmpty();
}

但有点难以理解。能简化吗?而不增加更多的新行?

英文:

Consider the following code. It is for check if the String has valid parenthesis but without using stack.

public boolean isValid(String input) {
       
    while(input.length() != (input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "")).length());
    return input.isEmpty();
}

But Kinda difficult to understand. Can this be simplified? Without adding more number of new lines?

答案1

得分: 6

public boolean isValid_2(String input) {
while (input.length() != (input = input.replaceAll("\(\)|\[\]|\{\}", "")).length())
;
return input.isEmpty();
}

Next, notice that the method doesn't depend on instances of its class, so can be static. Also, remove redundant escapes from the regex:

public static boolean isValid_3(String input) {
while (input.length() != (input = input.replaceAll("\(\)|\[]|\{}", "")).length())
;
return input.isEmpty();
}

Finally, break up the complicated statement into easy-to-understand parts, and introduce some variables with meaningful names, and then change the type of loop to something more useful, and you have your final version:

public static boolean isValid_4(String input) {
int oldLength, newLength;
do {
oldLength = input.length();
input = input.replaceAll("\(\)|\[]|\{}", "");
newLength = input.length();
} while (oldLength != newLength);
return input.isEmpty();
}

英文:

It helps if you first format and indent it properly:

public boolean isValid_2(String input) {
    while(input.length() != (input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "")).length())
        ;
    return input.isEmpty();
}

Next, notice that the method doesn't depend on instances of its class, so can be static. Also, remove redundant escapes from the regex:

public static boolean isValid_3(String input) {
    while(input.length() != (input = input.replaceAll("\\(\\)|\\[]|\\{}", "")).length())
        ;
    return input.isEmpty();
}

Finally, break up the complicated statement into easy-to-understand parts, and introduce some variables with meaningful names, and then change the type of loop to something more useful, and you have your final version:

public static boolean isValid_4(String input) {
    int oldLength, newLength;
    do {
        oldLength = input.length();
        input = input.replaceAll("\\(\\)|\\[]|\\{}", "");
        newLength = input.length();
    } while (oldLength != newLength);
    return input.isEmpty();
}

答案2

得分: 3

我的简化如下:

static boolean isValid(String input) {
    String t = input, s;
    do {
        s = t;
        t = s.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
    } while (s.length() != t.length());
    return t.isEmpty();
}

虽然这个版本较长,但我认为这样更容易理解代码的运作方式。我喜欢简洁,但并不总是最好的选择。

与其他简化答案不同的是,它更侧重于剩余的字符串而不是长度,这在我看来更加明了。但在某种程度上,这是审美的问题。

(另外,你可以在循环中的赋值之后方便地加上一个 "print",以查看实际发生了什么 - 我曾经这样做来调试我的错误注释)

英文:

My simplification is this:

static boolean isValid(String input) {
	String t = input, s;
	do {
		s = t;
		t = s.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
	} while (s.length() != t.length());
	return t.isEmpty();
}	

which, though longer, makes it easier IMO to see what's going on. I like brevity, but it's not always best.

This differs from other simplification answers in that it focuses more on the remaining strings than on the lengths, which to my mind is more to the point. But at some point, this is a matter of aesthetics.

(Also, you can conveniently stick a "print" after the assignments in the loop, to see what is really happening - I did this to debug my incorrect comment)

答案3

得分: 1

public boolean isValid(String input) {
    
    int prevLength = input.length();

    input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
    
    while(prevLength != input.length()) {
        prevLength = input.length();
        input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
    }
    
    return input.isEmpty();
}
英文:

Note: The question has been updated after I've answered the question. So, if doesn't fulfill the questions answer's each and every aspect, then please just ignore it.

let's see:

public boolean isValid(String input) {
    
    int prevLength = input.length();

    input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
    
    while(prevLength != input.length()) {
        prevLength = input.length();
        input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", "");
    }
    
    return input.isEmpty();
}

I guess its enough simplified...

huangapple
  • 本文由 发表于 2020年7月31日 19:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63191319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定