当前位置:网站首页>leetcode:20. Valid parentheses
leetcode:20. Valid parentheses
2022-08-06 03:11:00 【Soft-hearted and cool】
Difficulty: Easy
Given a string s that only includes '(', ')', '{', '}', '[', ']', determine whether the string is valid.
Valid strings must meet:
Opening parentheses must be closed with closing parentheses of the same type.
Open parentheses must be closed in the correct order.Example 1:
Input: s = "()"
Output: true
Example 2:Input: s = "()[]{}"
Output: true
Example 3:p>Input: s = "(]"
Output: false
Example 4:Input: s = "([)]"
Output: false
Example 5:Input: s = "{[]}"
Output: trueTip:
1 <= s.length <= 104
s consists only of brackets '()[]{}'Solution
class Solution:def isValid(self, s: str) -> bool:stack = []d = {')':'(','}':'{',']':'[',}if len(s) % 2 == 1:return Falsefor i in s:if i in d.values():stack.append(i)elif i in d.keys():if stack == [] or d[i] != stack.pop():return Falseelse:return Falsereturn not stack
边栏推荐
猜你喜欢

cocos小游戏实战-完结

Detailed description of hand-eye calibration (introduction of coordinate system, two-dimensional, three-dimensional hand-eye calibration method @ nine-point method, AX=XB)

03.变量

ALV details are reorganized2022.8.5

LeetCode:623. 在二叉树中增加一行【DFS or BFS】

django报错 ModuleNotFoundError: No module named 'mysqlclient'

数字化智能工厂解决方案47页

leetcode 15. Sum of three numbers

Android数据库框架之greenDAO

leetcode 17. 电话号码的字母组合
随机推荐
leetcode 18. Sum of Four Numbers
LeetCode Daily 2 Questions 01: Sliding Window - Longest Look Without Repeating Characters
Harbor offline synchronization
A tester in 1995, he wouldn't dare to ask for 12K~ Looking at his resume, I have a lot of thoughts...
R语言ggplot2循环中保存图片失败问题
学习MySQL的第一天:MySQL概述(基础篇)
Entering the pit of machine learning: 2. Supervised learning
vtk 补洞 两种实现
AC8015笔记
分享好用无广告的手机浏览器,亲测值得下载
详解AUTOSAR:AUTOSRA软件架构(理论篇—2)
LeetCode:623. 在二叉树中增加一行【DFS or BFS】
椭圆曲线介绍(四):椭圆曲线安全性,与RSA对比
美国国立卫生研究院(NIH)江晓芳组诚聘生物信息学博士后
九、MFC控件(一)
KU115 PCIE总线数据预处理板卡(多LVDS接口)
2022年京东新百货七夕礼遇季活动有什么亮点?
智能合约安全-整数溢出(SW101-IntegerOverflowAndUnderflow)
C Student Management System Print/Modify Designated Location Information
Preprocessing (in-depth understanding of C language)