[백준] #9012

팔랑이·2024년 1월 4일

BOJ

목록 보기
9/12


한번만에 맞춘게 뿌듯해서 올리는 글
근데 다른 답변 보니까 굳이 스택 안 만들고 리스트로 다들 풀더라...


👇🏻 스택 클래스를 굳이 만들어 사용한 코드

import sys
input = sys.stdin.readline

class Stack:
    def __init__(self):
        self.listStack = []
    
    def push(self, x):
        self.listStack.append(x)
    
    def pop(self):
        self.listStack.pop()
        
    def isEmpty(self):
        return not bool(self.listStack)

N = int(input())

for _ in range(N):
    braStack = Stack()
    braket = list(input().rstrip())
    try:
        for i in braket:
            braStack.push(1) if i == '(' else braStack.pop()

        print("YES" if braStack.isEmpty() == True else "NO")
       
    except:
        print("NO")




👇🏻 리스트 내장함수로 푼 코드

import sys
input = sys.stdin.readline

N = int(input())

for _ in range(N):
    braket = list(input().rstrip())
    braStack = []
    
    try:
        for i in braket:
            braStack.append(1) if i == '(' else braStack.pop()
               
        print("YES" if sum(braStack) == 0 else "NO") 

    except:
        print("NO")

사실상 위에 스택 구현부분만 빼면 코드는 똑같다..

profile
정체되지 않는 성장

0개의 댓글