강의

멘토링

커뮤니티

Inflearn Community Q&A

dch52220936's profile image
dch52220936

asked

Coding test top basic problems that are really easy to solve (with Java)

RemoveInvalidParentheses

isValid 함수 return 값 질문

Written on

·

1.2K

0

count 가 0이면 true를 리턴하고

0이 아니면 false를 리턴하도록 할때, 

return count == 0; 라고 하면 그렇게 되는건가요?

java코테 준비 같이 해요!

Answer 3

0

pushupman님의 프로필 이미지
pushupman
Instructor

네 맞습니다 그렇게 이해하시면됩니다

0

dch52220936님의 프로필 이미지
dch52220936
Questioner

함수의 타입형 자체가 boolean 이니 return count==0; 으로 해놓으면 count==0 일경우에만 true 가 return 되는거라고 이해하는게 맞나요?

0

pushupman님의 프로필 이미지
pushupman
Instructor

아래 부분 말씀하시는거 같은데, 말그대로 count==0인것만 true로 리턴하는거죠 ^^;

count 가 1 이거나 -1 나오면 false가 되느니거죠

private boolean isValid(String str) {

int count=0;

for(char c : str.toCharArray()) {

if(c =='(') {

count++;

}else if( c== ')') {

count--;

// ())(

if(count<0) return false;

}

}

return count==0;

}

dch52220936's profile image
dch52220936

asked

Ask a question