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: ...
随机推荐
- android 编译问题解决
1.android4.2.2 '/root/origin_android/mokesoures/out/target/common/obj/APPS/ApplicationsProvider_inte ...
- 给js对象赋值,赋值key
var pastResult = []; pastResult.push(feature.attributes.F_iID); pastResult.push(feature.attributes.F ...
- 深入浅出Stream和parallelStream
https://blog.csdn.net/darrensty/article/details/79283146
- 中面试中你不可回避的C、C++的问题(一)
基础中的基础 局部变量与全局变量问题 (使用’ ::’) 2. 如何在另个文件中引用一个全局变量 (extern) 3. 全局变量可以定义被多个C文件包含,并且是static 4. ...
- webstorm-----eslint的配置和使用
https://blog.csdn.net/qq_29329037/article/details/80100450
- iOS 流布局 UICollectionView使用(使用FlowLayout进行更灵活布局)
在UICollectionView的布局中,如果每个item的大小都一样那么是十分简单的事情,但是,如果我们想要的每个item大小不一样呢,这个时候,就要对UICollectionViewFlowLa ...
- 《Linux 鸟哥私房菜》 第一部分 Linux文件、目录与磁盘格式
1.Linux就是内核层与系统调用接口层这2层.
- 转 EBP ESP 的理解
PS:EBP是当前函数的存取指针,即存储或者读取数时的指针基地址:ESP就是当前函数的栈顶指针.每一次发生函数的调用(主函数调用子函数)时,在被调用函数初始时,都会把当前函数(主函数)的EBP压栈,以 ...
- opencv轮廓提取、轮廓识别相关要点
1.轮廓提取 src = cv2.imread("***.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src ,cv2.COLOR_BG ...
- [STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!
[STM8L]基于STM8L152的TAB段式LCD液晶驱动的分析 - 单片机干货 - 中国电子技术论坛 - 最好最受欢迎电子论坛!.md 主控芯片为STM8L152C4T6自带LCD控制器,低功耗系 ...