Python3解leetcode Valid Parentheses
问题描述:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
解题思路及方法:利用字典进行各个符号之间的配对;利用stack的先进后出特性进行验证
代码:
class Solution:
def isValid(self, s: str) -> bool:
if len(s)%2 != 0: return False
b = {'(':')', '[':']', '{':'}'}
stack = []
for x in s:
if x in b:
stack.append(x)
else:
if stack and x==b[stack.pop()]:
continue
else :
return False
if stack:
return False
else:
return True
Python3解leetcode Valid Parentheses的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- LeetCode——Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- Python3解leetcode Count Binary Substrings
问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
随机推荐
- linux支持的machine-types
在内核文件中arch/arm/tools/mach-types定义目前内核支持的板卡.芯片等: ##machine_is_xxx CONFIG_xxxx MACH_TYPE_xxx number ...
- HUAWEI HiAI亮相华为开发者生态大会 助力应用AI开发实现加速度
6月23日,在2018华为终端·全球合作伙伴及开发者大会AI分论坛体验区的一角,被层层叠叠的人群围得水泄不通.站在最前面的一名体验者,正跟随着“快手短视频”APP上不断出现的小人左右扭动,每完成一个动 ...
- 深入Asyncio(十一)优雅地开始与结束
Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...
- ajax学习笔记(3)--$.get()方法
<head runat="server"> <title>jQuery中的$.get()方法</title> <script src=&q ...
- 九度OJ 1051:数字阶梯求和 (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6718 解决:2280 题目描述: 给定a和n,计算a+aa+aaa+a...a(n个a)的和. 输入: 测试数据有多组,输入a,n(1&l ...
- 九度OJ 1033:继续xxx定律 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4987 解决:1201 题目描述: 当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数, ...
- ios Symbol(s) not found for architecture arm64总结 含隐藏错误cocoapods
一.通用 报错:Desktop/project/ASDF/WEIXIN/libWeChatSDK.a (3 slices) Undefinedsymbols for architecture arm6 ...
- Django—工程创建以及models数据库易错点
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- AnkhSVN
安装和配置 签入签出问题 1.安装和配置 ①安装.(貌似默认的安装到C:\Program Files\AnkhSVN 2下,开始菜单也没快捷?) ②源代码管理器设置:打开vs2012,工具→选项→源代 ...