[Leetcode] valid parentheses 有效括号对
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)
{
if(s.empty()) return true;
stack<char> stk;
for(int i=;i<s.size();++i)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
stk.push(s[i]);
else
{
if( stk.empty())
return false;
char temp=stk.top();
stk.pop();
if((temp=='('&&s[i] !=')')||(temp=='['&&s[i] !=']')||(temp=='{'&&s[i] !='}'))
return false;
} }
return stk.empty();
}
};
括号的题还有generate parentheses 、longest valid parentheses
[Leetcode] valid parentheses 有效括号对的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- [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 ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] 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 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- String、StringBuffer、StringBuilder的区别和解析
1.三个类之间的关系 他们都是通过字符数组来实现的,继承关系 String:字符串常量,不可变类 StringBuffer:字符串变量,可变类,线程安全 StringBuilder:字符串变量,可变类 ...
- JAVA 面试须知
本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺. 1. Java中的原始数据类型都有哪些, ...
- win7下本地运行spark以及spark.sql.warehouse.dir设置
SparkSession spark = SparkSession .builder() .master("local[*]") .enableHiveSupport() .con ...
- 深入理解 Vuejs 动画效果
本文主要归纳在 Vuejs 学习过程中对于 Vuejs 动画效果的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 V ...
- 关于C#中如何使用wmi获得操作系统信息?
最近项目中用到了windows server 2012操作系统中的存储池和ISCSI Disk的技术.前期,我们整个操作都是用power shell脚本去实现了.带来了不方便,后期要使用wmi API ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- rsync+inotify实现实时同步,自动触发同步文件
本文参考来自:http://chocolee.blog.51cto.com/8158455/1400596 我的需求和他的略有不同,同时做了一下更改,如下: 需求:两台机器相互为主备,搭建相同的两个服 ...
- Windows网络编程系列教程之四:Select模型
讲一下套接字模式和套接字I/O模型的区别.先说明一下,只针对Winsock,如果你要骨头里挑鸡蛋把UNIX下的套接字概念来往这里套,那就不关我的事. 套接字模式:阻塞套接字和非阻塞套接字.或者叫同步套 ...
- Qt代码覆盖率code coverage(VS版)
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt代码覆盖率code coverage(VS版) 本文地址:http://techi ...
- week1 四人小组项目
小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 项目选题:东北师范大学论坛 作为东北师范大学同学间的信息交流平台,要满足的需求如下: 1.校内信息及公告 2.毕业生招聘信息 3.课程查 ...