알고리즘
(백준) 4949번 : 균형잡힌 세상 - 자바[JAVA]
코딩개발
2021. 9. 14. 17:13
728x90
반응형
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class Test_4949 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Stack<Character> stack = new Stack<>();
String str;
String result = "Yes";
while(true) {
str = br.readLine();
if(str.equals(".")) {
break;
}
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(ch == '(' || ch == '[') {
stack.push(ch);
} else if(ch == ')') {
if(stack.isEmpty() || stack.peek() != '(') {
result = "No";
} else {
stack.pop();
}
} else if(str.charAt(i) != ']') {
if(stack.isEmpty() || stack.peek() != '[') {
result = "No";
} else {
stack.pop();
}
}
if(stack.empty()) {
result = "Yes";
} else {
result = "No";
}
}
sb.append(result + "\n");
}
System.out.println(sb);
}
}
result에 값을 주지않고 바로 값을 출력하는 방법을 사용하는 것이 더 좋은 방법으로 보임
"입력의 종료조건으로 맨 마지막에 점 하나(".")가 들어온다."의 의미가
So when I die (the [first] I will see in (heaven) is a score list). 마지막에 있는 점으로 생각했는데
다른 분들의 코드를 보니
예제입력 마지막 줄에 점 하나(".")가 있는 경우를 의미하는 것으로 보인다.
728x90
반응형