Eliza1130的专栏

Given a string containing just the characters'(‘,’)’,'{‘,’}’,'[‘and’]’, determine if the input string is valid.

The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.

class Stack:#定义栈 后进先出data=[]def push(self,item):self.data.append(item)def pop(self):if self.data == []:return Falseelse:return self.data.pop()def length(self):return len(self.data)def clear(self):self.data=[]class Solution:# @return a booleandef isValid(self, s):p={'(':')','[':']','{':'}'}stack=Stack()stack.clear()for c in s:if c in p.keys():stack.push(c)elif c in p.values():k=stack.pop()if k==False or p[k]!=c:return Falseelse:return Falseif stack.length() == 0:return Trueelse:return False

,而消极的人则在每个机会都看到某种忧患。

Eliza1130的专栏

相关文章:

你感兴趣的文章:

标签云: