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: ...
随机推荐
- 在dev目录创建一个字符设备驱动的流程
1.struct file_operations 字符设备文件接口 1: static int mpu_open(struct inode *inode, struct file *file) 2: ...
- alibaba canal安装笔记
canal是alibaba开源的基于mysql binlog解析工具,可利用它实现mysql增量订阅/消费,典型的应用场景如下图: 利用canal,可以将mysql的数据变化,通过解析binlog,投 ...
- hadoop基础----hadoop理论(四)-----hadoop分布式并行计算模型MapReduce具体解释
我们在前一章已经学习了HDFS: hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详细解释 我们已经知道Hadoop=HDFS(文件系统,数据存储技术相关)+ ...
- python 基础 3.1 打开文件 a a+ r+ w+ 详解
一.python 访问文件 1.在python中要访问文件,首先要打开文件,也就是open ---open r: 只读 w: 只写 ,文件已存在则清空,不存在则创建 a:追加 ...
- authority分层
- EasyDSS RTMP流媒体服务器的HTTP接口query url的C++实现方法
EasyDSS支持HTTP GET接口访问,我们需要获取url的各种参数信息 比如http://ip:port/action?a=1&b=2&c=3 我们需要知道对应的a.b.c的值 ...
- 关于Wix的源代码
Wix的源代码有两种方式可以获得,以3.8为例: 在Release的页面下载wix38-debug.zip 通过SourceCode页面下载,http://wix.codeplex.com/Sourc ...
- When Programmers and Testers Collaborate
When Programmers and Testers Collaborate Janet Gregory SOMETHING MAGICAL HAPPENS when testers and pr ...
- java参数传递------真心是值传递
未完待续 不同意的请尽管去深入看一下. 对象是引用传递没错,参数传递是值传递.
- render 的执行流程
流程 : render 只能识别 字符串,对于其他的css,html,js,jquery样式是不能识别的,它会将文件中的内容解析称为字符串拿到前端页面,浏览器进行渲染. 例如 : # 视图函数 de ...