题目:

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. Ubuntu14.04进入文本模式方法

    ubuntu 10.10以前的操作方法: 1 第一步,具体命令及操作如下: sudo vi /etc/init/rc-sysinit.conf env DEFAULT_RUNLEVEL=3 <- ...

  2. placeholder插件及placeholder默认颜色修改

    $.fn.placeHolder = function(){ $(this).each(function(i, el) { var self = $(el); if ($.browser.msie & ...

  3. GridView 鼠标经过时变色两种方法

    第一种: 新建一个js文件 并引用 <script src="jquery.js" type="text/javascript"></scri ...

  4. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  5. 【积硅计划】html标签

    一.基础     在html中,标签通常是由开始标签和结束标签组成,开始标签用<标签名>,结束标签用</标签名>      元素是指包括开始标签和结束标签在内的整体,内容是指出 ...

  6. DevExpress 中 WaitForm 使用

    第一步: 在程序中拖入: splashScreenManager1 控件 在需要处理的地方 使用以下语句来打开 WaitForm窗体(当然需要在 splashScreenManager1控件中绑定一个 ...

  7. How to setup and process Intercompany accounting [AX2012]

    In this post, I will take you through a very simple functionality called  the intercompany accountin ...

  8. 基于OWIN WebAPI

    http://www.cnblogs.com/Irving/p/4607104.html http://www.cnblogs.com/xishuai/p/aspnet-webapi-owin-oau ...

  9. for xml path('') 引发的数据不完整

    When you read Extensible Markup Language (XML) data from Microsoft SQL Server by using the SqlDataRe ...

  10. GraphLab面向机器学习的并行框架『针对图数据处理模型』

    最近在做文本处理知识的梳理,关注了CMU提出的GraphLab开源分布式计算系统 这是关于GraphLab的PPT:Distributed GraphLab『 http://cheng-qihang- ...