20. Valid Parentheses
来源:互联网 发布:邢帅表白app源码下载 编辑:程序博客网 时间:2024/06/07 23:19
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.
思路分析:这是一道比较典型的利用堆栈去解决的题目,将符号逐一压入到栈中,遇到相匹配的结果就弹出即可。下面附上代码。
class Solution {public: bool isValid(string s) { stack <char> charStack; int i=0; while(i!=s.length()){ char c=s[i]; if(c!=')'&&c!=']'&&c!='}'){ charStack.push(c); } else{ if(charStack.size()==0) return false; char d=charStack.top(); switch(c){ case ')': if(d=='(') charStack.pop(); else return false; break; case ']': if(d=='[') charStack.pop(); else return false; break; case '}': if(d=='{') charStack.pop(); else return false; break; } } i++; } if(charStack.size()==0) return true; else return false; }};
0 0
- LeetCode 20. Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- LeetCode --- 20. Valid Parentheses
- LeetCode 20.Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- 20.Valid Parentheses
- 20.Valid Parentheses
- 20. Valid Parentheses
- 【leetcode】20. Valid Parentheses
- [leetcode] 20.Valid Parentheses
- [leetcode] 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- 20. Valid Parentheses
- 20. Valid Parentheses
- eclipse下tomcat中文乱码解决
- 2017浙江省赛:Yet Another Game of Stones(尼姆变形)
- Android面试
- 解决 nginx server (ubuntu)上安全漏洞 OpenSSL AES-NI Padding Oracle MitM Information Disclosure
- (leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
- 20. Valid Parentheses
- servlet中list转换成一个歌的对象
- New study shakes the roots of the dinosaur family tree
- 自己写的UiUtils工具类
- Java编程思想重点笔记(Java开发必看)
- 堆(heap)和栈(stack)、内存泄漏(memory leak)和内存溢出
- HTTP协议详解(转载)
- 修改hosts--目标文件夹访问拒绝解决办法
- RapidMiner 异常/随机