http://oj.leetcode.com/problems/valid-parentheses/

对栈的考察,看括号的使用方式是否合法。

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

LeetCode OJ--Valid Parentheses的更多相关文章

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

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

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

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

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

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

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

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

  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 ...

  10. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

随机推荐

  1. CentOS7——防火墙设置

    1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态firewall-cmd --state 3.开启.重启.关闭.firewalld ...

  2. C++函数调用过程深入分析<转>

    转自http://blog.csdn.net/dongtingzhizi/article/details/6680050 C++函数调用过程深入分析 作者:靠谱哥 微博:洞庭之子-Bing 0. 引言 ...

  3. 新数据的GT列表

    制作新数据集时需要重新制作train_GT,test_GT 代码: dic = {} with open('/home/bnrc/all_image_GT.txt','r') as file: for ...

  4. Hermite 矩阵的特征值不等式

    将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论.   Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...

  5. tp5 -- 微信公众号支付

    近来期间比较忙, 忙完之后发现最近有挺多的东西没有整理,于是乎.就将以前用到的一些小东西整理了一下. 如果对您有帮助,则是我最大的幸运. 本篇主要是说了一下整合TP5的微信公众号支付. 不过由于最近T ...

  6. viewDidLoad、viewWillAppear、viewWillDisappear

    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil viewDidLo ...

  7. Linux中的定时任务简单操作实例

    今天,我怀着一颗感恩的心,写了一个小小的定时任务,细想还是写个简单的例子吧,希望能帮到你! 首先我在/usr/local/文件夹下创建了一个文件,hah.sh文件 在hah.sh里面编写shell脚本 ...

  8. 图论:POJ2186-Popular Cows (求强连通分量)

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  9. windows10系统下安装keras框架以theano为后端并配置gpu加速

    在安装之前,请确保你的显卡是NVIDIA的,并且是以下型号,否则不能进行gpu加速,右键我的电脑--管理--设备管理器--显示适配器.另外如果你的电脑是windows7,安装教程也是一样的,不过根据k ...

  10. Experiments done

    喷重金属 换重金属溶液 荧光光合 备注 ASD 备注 高光谱 备注 泡EDTA 备注 电镜 备注 2018.12.19(day1) 2018.12.19(day1) 2018.12.18晚(day0) ...