[Prgms_week1] 올바른 괄호

GREEN·2021년 5월 2일
0

Algorithm

목록 보기
1/14
post-thumbnail

올바른 괄호

🔈 첫번째 풀이

def solution(s):
    stac = []
    for x in s:
        print(stac)
        if x == '(':
            stac.append(x)
        else:
            if not stac:
                return False
            else:
                stac.pop()
    if stac:
        return False
    return True

🔉 코드리뷰를 가진 풀이

  1. 오퍼레이터 사이는 띄워야 한다.
if stac:
    return False
return True

위 코드를 아래로 수정

return if stac == []
def solution(s):
    stack = []
    for x in s:
        if x == '(':
            stack.append(x)
        else:
            if not stack:
                return False
            else:
                stack.pop()
    return if stack == []

🔊 리더님 코드

def solution(s):
    c = 0
    for x in s:
        if x == '(':
            c += 1
        else:
            c -= 1
        if c < 0:
            return False
    return c == 0
profile
초록도치

0개의 댓글