Last active
December 23, 2021 10:49
-
-
Save twsiyuan/4d2f5565fedb9d4984199dcd7f6642c6 to your computer and use it in GitHub Desktop.
LeetCode #20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/problems/valid-parentheses/ | |
class Solution: | |
def isValid(self, s): | |
""" | |
:type s: str | |
:rtype: bool | |
""" | |
stack = [] | |
for c in s: | |
if c == ")" or c == "]" or c == "}": | |
if len(stack) <= 0: | |
return False | |
else: | |
c2 = stack.pop() | |
if not((c == ")" and c2 == "(") or (c == "]" and c2 == "[") or (c == "}" and c2 == "{")): | |
stack.append(c2) | |
stack.append(c) | |
else: | |
stack.append(c) | |
return len(stack) <= 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is a best solution found that very popular and helpful:
https://www.youtube.com/watch?v=-3S5gH2cuYw&ab_channel=EricProgramming