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.

见了n次的括号匹配,难度不大,主要使用栈来进行匹配测试,但是记得检测一些边缘的情况,例如一个[,或者一个]时,怎么判断,怎么处理?

class Solution {
public:
bool isValid(string s)
{
stack<char> stk;
int nSize = s.size(); for(int i = ; i!= nSize; ++i)
{
if(s[i] == '{' || s[i] == '(' || s[i]== '[')
{
stk.push(s[i]);
}
else
{
if(stk.empty())
{
return false;
}
char cElem = stk.top();
if(cElem - s[i] ==- || cElem - s[i] == -)
{
stk.pop();
continue;
}
else
{
return false;
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
};

希望下次见到,还是能一次性AC过!!!

LeetCode 19 Valid Parentheses的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

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

  2. [LeetCode] Longest Valid Parentheses 解题思路

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

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  5. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  8. [LeetCode] Longest Valid Parentheses 动态规划

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

  9. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. spring cloud 之 客户端负载均衡 Ribbon

    一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...

  2. TCP/IP三次握手与四次握手

    原文地址 http://blog.csdn.net/whuslei/article/details/6667471 http://blog.csdn.net/wo2niliye/article/det ...

  3. 【.Net】Visual Studio的调试技巧

    这是我写的关于VS2010和.Net4发布的博客系列的第26篇. 今天的博文包含了一些有用的能用于VS的调试技巧. 我的朋友Scott Cate(他写了很多很好的关于VS使用技巧和窍门的博客)最近向我 ...

  4. 【Todo】【转载】JVM学习

    先参考如下这个系列<聊聊JVM> http://blog.csdn.net/column/details/talk-about-jvm.html

  5. Go语言【第二篇】:Go语法和数据类型

    Go语言基础语法 Go标记 Go程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下Go语句由6个标记组成: fmt.PrintIn("Hello, World!&quo ...

  6. [APIO2015]巴厘岛的雕塑 贪心+DP+特殊数据优化

    写了好久.... 刚刚调了一个小时各种对拍,,,,最后发现是多写了一个等号,,,,内心拒绝 表示一开始看真的是各种懵逼啊 在偷听到某位大佬说的从高位开始贪心后发现可做 首先考虑小数据(因为可以乱搞) ...

  7. Android ListView 显示多种数据类型

    ListView往往可能会有不同的数据类型,单类型的数据可能运用会比较少些,这也是最近项目中的一个需求{在发送消息的时候,需要选择联系人,而联系人列表由英文字母索引+联系人组成},上一篇文章只是一个基 ...

  8. BZOJ3143:[HNOI2013]游走——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3143 Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随 ...

  9. HDOJ.1228 A + B (map)

    A + B 点我挑战题目 点我一起学习STL-MAP 题意分析 讲字符串和数字用map对应即可 代码总览 /* Title:HDOJ.1228 Author:pengwill Date:2016-11 ...

  10. bzoj1968: [Ahoi2005]COMMON 约数研究(数论)

    计算每一个数的贡献就好了..O(N) n/i只有2*sqrtn个取值于是可以优化到O(sqrtn) #include<bits/stdc++.h> #define ll long long ...