[LeetCode] Valid Parentheses

CHOI YUN HOยท2021๋…„ 7์›” 10์ผ
0

๐Ÿ“ƒ ๋ฌธ์ œ ์„ค๋ช…

Valid Parentheses

[๋ฌธ์ œ ์ถœ์ฒ˜ : LeetCode]

๐Ÿ‘จโ€๐Ÿ’ป ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๊ทธ๋ƒฅ ๊ด„ํ˜ธ์˜ ์ง์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์Šคํƒ์„ ์ด์šฉํ•˜๊ณ 
์—ด๋ฆฐ ๊ด„ํ˜ธ์™€, ๋‹ซํžŒ ๊ด„ํ˜ธ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์กฐ๊ฑด์— ๋”ฐ๋ผ
push, popํ•œ๋‹ค.

๐Ÿ‘จโ€๐Ÿ’ป ์†Œ์Šค ์ฝ”๋“œ

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """

        stack = []

        for c in s:
            if len(stack) == 0 and c in [')', '}', ']']:
                return False
            elif c in ['(', '{', '[']:
                stack.append(c)
            elif c == ')':
                if stack[-1] == '(':
                    stack.pop(-1)
                else:
                    return False
            elif c == '}' :
                if stack[-1] == '{':
                    stack.pop(-1)
                else:
                    return False
            elif c == ']' :
                if stack[-1] == '[':
                    stack.pop(-1)
                else:
                    return False

        if len(stack) == 0:
            return True
        else:
            return False
profile
๊ฐ€์žฌ๊ฐ™์€ ์‚ฌ๋žŒ

0๊ฐœ์˜ ๋Œ“๊ธ€