123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import pytest
- def isValid(s: str):
- lens = len(s)
- if lens % 2 == 1:
- return False
- dic = {
- "(": ")",
- "[": "]",
- "{": "}"
- }
- # 栈
- list = []
- for i in s:
- if i in dic.keys():
- # 入栈
- list.append(i)
- else:
- # 出栈
- if len(list) == 0 or i != dic.get(list[-1]):
- list.append(i)
- else:
- list.pop()
- # 栈为空。意为括号都匹配结束
- if len(list) == 0:
- return True
- else:
- return False
- @pytest.mark.parametrize(
- "s, expect",
- [
- ("()[]", True),
- ("(({[])}", False),
- ("(]", False),
- ("([{}])", True),
- ("21[o{00}j]", False)
- ]
- )
- def test_cases(s, expect):
- assert isValid(s) == expect
|