[LeetCode] 20. Valid Parentheses_Easy tag: Stack
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, 不能用count了, 因为{[}]是invalid, 但是count没法判断. 那么为了elegant, 建一个hash table, d = {']':'[', '}':'{', ')':'('}, 如果在d里面, 就通过判断stack是否为空和stack.pop() 是否跟d[c] 相等,
如果在d.values()里面, 就append进入stack.
Code: T: O(n) S; O(n)
class Solution:
def validParenthesis(self, s):
stack, d = [], {']':'[', '}':'{', ')':'('}
for c in s:
if c in d and (not stack or stack.pop() != d[c]):
return False
elif c in d.values():
stack.append(c)
return not stack
[LeetCode] 20. Valid Parentheses_Easy tag: Stack的更多相关文章
- [Leetcode] 20. Valid Parentheses(Stack)
括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- Leetcode 20 Valid Parentheses stack的应用
判断括号是否合法 1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈 2.判断stack是否为空,可以判断是否合法 class Solution { public: bool is ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- [LeetCode] 394. Decode String_Medium tag: stack 666
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 20. Valid Parentheses(stack)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- springboot面试题总结
什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的s ...
- 【TOP100案例专访】当当网工程师林嘉琦谈双11大促经验及APM实践
导读:第七届TOP100全球软件案例研究峰会将于11月30日-12月3日在北京国家会议中心举办,本届峰会以“释放AI生产力 让组织向智能化演进”为开幕式主题,旨在推动企业在趋势下拥抱AI.探索和思考A ...
- Hive日志(Hive Logging)--hive GettingStarted翻译
Hive uses log4j for logging. By default logs are not emitted to the console by the CLI. The default ...
- python3写入csv文件时中文为乱码
今天修改李万的爬虫时把页面上的中文写入csv文件时,中文总是乱码.通过上网搜索得到解决.解决的办法是打开文件是需加参数 encoding='utf-8-sig' .感谢博客园的菜鸟Alex.他相关博客 ...
- 查询执行成本高(查询访问表数据行数多)而导致实例 CPU 使用率高是 MySQL 非常常见的问题
MySQL CPU 使用率高的原因和解决方法_产品性能_常见问题_云数据库 RDS 版-阿里云 https://help.aliyun.com/knowledge_detail/51587.html ...
- [tcpreplay] tcpreplay高级用法--使用tcpreplay-edit进行循环动态发包
tcpreplay-edit提供了可对包进行修改的高级用法: --unique-ip Modify IP addresses each loop iteration to generate uniqu ...
- mysql学习【第2篇】:MySQL数据管理
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! mysql学习[第2篇]:MySQL数据管理 外键管理 外键概念 如果公共关键字在一个关系中是主 ...
- AES加解密所遇问题
AES加解密后解密数据末尾携带多余空格,经查看是由于加密时数据不足16个字节自动补齐导致 解决办法:记录加密数据长度,解密后根据数据长度读取解密数据. 另外加密数据中可能存在0等数据,所以拷贝内容时最 ...
- POJ2274 Long Long Message 字符串
正解:SA/哈希+二分 解题报告: 传送门! 啊先放下翻译,,,?大意就有两个字符串,求这两个字符串的最长公共子串 先港SA的做法趴 就把两个子串拼接起来,然后题目就变成了求后缀的最长公共前缀了 所以 ...
- UOJ244 短路 贪心
正解:贪心 解题报告: 传送门! 贪心真的都是些神仙题,,,以我的脑子可能是不存在自己想出解这种事情了QAQ 然后直接港这道题解法趴,,, 首先因为这个是对称的,所以显然的是可以画一条斜右上的对角线, ...