Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.03.12 - [BOJ] 9012. 괄호 #31

Closed
suhyunsim opened this issue Mar 11, 2021 · 0 comments
Closed

21.03.12 - [BOJ] 9012. 괄호 #31

suhyunsim opened this issue Mar 11, 2021 · 0 comments
Assignees
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Mar 11, 2021

문제

핵심 아이디어

  • 수식의 괄호 쌍 기본 문제

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q9012;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            boolean flag = true;
            String line = br.readLine();
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (c == '(') {
                    stack.push(c);
                } else if (c == ')') {
                    if (!stack.isEmpty() && stack.pop() == '(') {
                        continue;
                    } else {
                        flag = false;
                        break;
                    }
                }
            }
            if (!stack.isEmpty() || !flag) {
                System.out.println("NO");
            } else {
                System.out.println("YES");
            }
        }
        br.close();
    }
}
@suhyunsim suhyunsim added 실버 BOJ - 실버 스택 stack labels Mar 11, 2021
@suhyunsim suhyunsim added this to the 3월 2주 차 milestone Mar 11, 2021
@suhyunsim suhyunsim self-assigned this Mar 11, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant