Leetcode(19)
来源:互联网 发布:网络社交的利弊二辩 编辑:程序博客网 时间:2024/05/17 05:14
https://leetcode.com/problems/valid-parentheses/#/description
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
Solution:
经典的匹配题,用stack来解决。
bool ValidParentheses::isValid(std::string s) { std::stack<char> leftParentheses; for (char c : s) { if (c == '(' || c == '[' || c == '{') { leftParentheses.push(c); } else if (c == ')' || c == ']' || c == '}') { char leftParenthese = ' '; switch (c) { case ')': leftParenthese = '('; break; case ']': leftParenthese = '['; break; case '}': leftParenthese = '{'; break; } if(leftParentheses.empty() || leftParentheses.top() != leftParenthese) { return false; } leftParentheses.pop(); } } return leftParentheses.empty();}
阅读全文
0 0
- Leetcode 19
- Leetcode(19)
- leetcode 19 和leetcode 83
- leetcode 19: Edit Distance
- [leetcode 19] N-Queens
- LeetCode 19 Clone Graph
- LeetCode (19) Multiply Strings
- 【leetcode c++】19 removeNthFromEnd
- leetcode之-题19
- leetcode #19 in cp
- leetCode练习(19)
- (java)leetcode-19
- Leetcode刷题(19)
- [Leetcode] 82, 61, 19
- leetcode 19 removeNthFromEnd
- 4.19 leetcode -19 candy
- LeetCode刷题(19)
- leetcode
- 【旧资料整理】(DEMO SHOW)花样扫雷(RichMine)新界面
- ugui的优化
- Linux下修改MySQL初始密码、开启远程登录、授权远程登录用户
- Markdown转PPT的方法
- kubeadm安装kubernetes(weave)
- Leetcode(19)
- Spring Boot学习(RESTful API)
- 闪讯无线助手-Android端闪讯破解路由器拨号实现原理
- php 的 cli 运行模式
- 安卓开发,与后台服务器对接中,JSON解析入门学习
- 【旧资料整理】MFC--自己优化的双缓冲绘图方法
- MVC权限设计
- Python 读取csv的某行——两种方法
- ora-01480 str 绑定值的结尾null字符缺失