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: ...
随机推荐
- erlang进程监控:link和monitor
Erlang最开始是为了电信产品而发展起来的语言,因为这样的目的,决定了她对错误处理的严格要求.Erlang除了提供exception,try catch等语法,还支持Link和Monitor两种监控 ...
- 【caffe】Caffe的Python接口-官方教程-01-learning-Lenet-详细说明(含代码)
01-learning-Lenet, 主要讲的是 如何用python写一个Lenet,以及用来对手写体数据进行分类(Mnist).从此教程可以知道如何用python写prototxt,知道如何单步训练 ...
- WCF: 以Json格式返回对象
1.先建一个WCF Service 建一个ServiceContract接口 1 [ServiceContract] public interface IJsonWCFService { /// &l ...
- TCP的四种定时器简单记录
TCP管理的4个不同的定时器: 1.重传定时器:用于当希望收到另一端的确认. 2.坚持定时器:使窗口大小信息保持不断流动. 3.保活定时器:检测TCP空闲连接的另一端何时崩溃或重启. 4.2MSL定时 ...
- Darwin做直播时对ReflectorSession引用数的控制
在之前的博客中,我们提到了如何用Darwin&live555实现直播过程,那么更进一步,当直播结束时,我们需要关闭所有正在收看的客户端,并且delete转发会话ReflectorSession ...
- pycharm注册码地址
(1)地址:http://idea.lanyus.com/ (2)注意,在破解的时候,是先修改hosts文件所在路径:“C:\Windows\System32\drivers\etc\hosts”,修 ...
- ES5中的类与继承
最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...
- 题解 P3372 【【模板】线段树 1】
发一篇不需要O2就能过的分块. 基本思路: 分块的思路,大段维护,小段朴素. 维护几个数组: 区块\(block[maxn]\) 懒标记\(tag[maxn]\) 真实数据\(data[maxn]\) ...
- AsyncTask==Handler+Thread对比使用说明
AsyncTask能够合理且轻松使用UI线程,该类允许执行后台操作和发送结果到UI线程而不需要操作threads或handlers. AsyncTask是针对Thread和Handler代替而封装好的 ...
- Rocketmq消息持久化
本文编写,参考:https://my.oschina.net/bieber/blog/725646 producer Send()的Message最终将由broker处理,处理类为:SendMessa ...