题目:

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) {
std::stack<char> result;
for ( size_t i = ; i < s.length(); ++i )
{
if ( s[i]=='(' || s[i]=='[' || s[i]=='{' )
{
result.push(s[i]);
continue;
}
if ( result.empty() ) return false;
char top = result.top();
if ( s[i]==')' && top!='(' ) return false;
if ( s[i]==']' && top!='[' ) return false;
if ( s[i]=='}' && top!='{') return false;
result.pop();
}
return result.empty();
}
};

tips:

口诀:左号进栈,右号出栈;出栈判断是否正确。

===========================================

第二次过这道题,凭感觉写了一份代码,稍微冗长一些;修改了一次AC了。

class Solution {
public:
bool isValid(string s) {
stack<char> sta;
int i = ;
while ( i<s.size() )
{
if ( s[i]=='(' || s[i]=='[' || s[i]=='{' )
{
sta.push(s[i]);
}
else if ( s[i]==')' || s[i]==']' || s[i]=='}' )
{
if ( sta.empty() ) return false;
if ( s[i]==')' )
{
if ( sta.top()=='(' )
{
sta.pop();
}
else
{
return false;
}
}
else if ( s[i]==']' )
{
if ( sta.top()=='[' )
{
sta.pop();
}
else
{
return false;
}
}
else
{
if ( sta.top()=='{' )
{
sta.pop();
}
else
{
return false;
}
}
}
else
{
return false;
}
++i;
}
return sta.empty();
}
};

有个地方不要遗漏:如果是右号,要判断一下stack是否为空。

【Valid Parentheses】cpp的更多相关文章

  1. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  2. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. 【Valid Palindrome】cpp

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  4. 【Generate Parentheses】cpp

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  5. 【Valid Number】cpp

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...

  6. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  7. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  8. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  9. 【Gray Code】cpp

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

随机推荐

  1. [原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

    本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1) ...

  2. [原]SQL_实验2.1.3 清华大学出版社

    本文出自:http://blog.csdn.net/svitter 实验目标:熟悉实体完整性,参照完整性,事务的处理: /*1.在数据库school表中建立表Stu_uion,进行主键约束,在没有违反 ...

  3. css style与class之间的区别,cssclass

    问题描述:    网页点击[导出]按钮后,将页面table内容另存成excel文件,却发现无法保存表格样式 分析过程: 1.table表格用class,而不是style.导出时并没有导出class定义 ...

  4. 为什么swing不适合做桌面软件

    http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...

  5. 常规轮询请求,客户端用Ajax调webservice的方法

    服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...

  6. mac配置iterm2和ohmyzsh

    点击我直通 iTerm2下载 利用命令下载ohmyzsh curl -L http://install.ohmyz.sh | sh 安装好之后,运行 vi ~/.zshrc 可以查看到以下配置内容信息 ...

  7. LotusPhp入口文件解析

    LotusPhp也是单入口的框架,可以根据需要开启多个应用实例 例如前台页面可以用index.php作为入口文件,后台可以用admin.php作为入口文件,多个应用实例可以共享应用配置和类库或者根本每 ...

  8. android空鼠修改

    抛弃盒子自带遥控器后,又不满意改键红外遥控器,选择飞鼠及无线键鼠成为最终方案.问题是:菜单键如何实现!其实很简单:即插即用USB无线飞鼠及键鼠套装只涉及2个文件:system/usr/layout/G ...

  9. FileInputStream利用缓冲数组读取数据

    package cd.itcast.fileinputstream; import java.io.File; import java.io.FileInputStream; import java. ...

  10. Css 书写规范【转】

    1. 不同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容. /** 清除内外边距 **/ body, h1, h2, h3, h4, h5, h6, h ...