数据结构中,第9天学习了栈和队列,这两种数据结构有何区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计344个文字,预计阅读时间需要2分钟。
pythonclass Solution: def isValid(self, s: str) -> bool: stack=[] for char in s: if char=='(' or char=='{' or char=='[': stack.append(char) else: if not stack: return False top=stack.pop() if (char==')' and top !='(') or \ (char=='}' and top !='{') or \ (char==']' and top !='['): return False return not stack
20. 有效的括号
判断输入的括号是否有效。 左右括号··能闭合,顺序合适。
思路:用栈实现。遇到左括号就保存在栈中,遇到右括号则需要从栈中弹出一个括号,与之配对。
本文共计344个文字,预计阅读时间需要2分钟。
pythonclass Solution: def isValid(self, s: str) -> bool: stack=[] for char in s: if char=='(' or char=='{' or char=='[': stack.append(char) else: if not stack: return False top=stack.pop() if (char==')' and top !='(') or \ (char=='}' and top !='{') or \ (char==']' and top !='['): return False return not stack
20. 有效的括号
判断输入的括号是否有效。 左右括号··能闭合,顺序合适。
思路:用栈实现。遇到左括号就保存在栈中,遇到右括号则需要从栈中弹出一个括号,与之配对。

