大家好,我是毛毛。ヾ(´∀ ˋ)ノ
那就開始今天的解題吧~
Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
1 <= s.length <= 10^4
s
consists of parentheses only '()[]{}'
.class Solution:
def isValid(self, s: str) -> bool:
stack = []
pointer = -1
for index in range(len(s)):
print("Now s: ", s[index])
ㄌ
if s[index] == ')' or s[index] == ']' or s[index] == '}':]
if index == 0:
return False
else:
if len(stack) == 0:
return False
else:
bracket = stack.pop(-1)
if (bracket == '(' and s[index] == ')') or (bracket == '[' and s[index] == ']') or (bracket == '{' and s[index] == '}'):
pointer -= 1
else:
return False
if s[index] == '(' or s[index] == '[' or s[index] == '{':
stack.append(s[index])
pointer += 1
if len(stack) != 0:
return False
return True
今天就到這邊啦~
大家明天見