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. nyoj-586-疯牛|poj-2456-Aggressive cows

    http://acm.nyist.net/JudgeOnline/problem.php?pid=586 http://poj.org/problem?id=2456 解题思路:最大化最小值二分答案即 ...

  2. 通过工厂模式批量创建对象后调用其中方法 出现XXXis not a function()问题原因

    //通过工厂模式批量创建 function Computer(color,weight,logo){         var obj=new Object();         obj.color=c ...

  3. CF-1082(渣渣只做了前三个)

    链接:http://codeforces.com/contest/1082 A. Vasya and Book 题意: n,x,y,d 一本电子书有n页,每一次翻动只能往前或者往后翻d页.求x-> ...

  4. (27)zabbix自定义图表Graph

    zabbix提供了一个自定义图表的功能,这不是废话么?呵呵~前面文章 讲到的<zabbix简易图表>只能显示单个item的数据图表.如果我们想显示多个信息到一个图表上,那必须使用zabbi ...

  5. asyn_fifo

    //Module Name:afifo_ctrl //Description:parameterized afifo module afifo_ctrl( clk_push, rst_push_n, ...

  6. HTML 文件类表单元素如何限制上传类型,Accept属性设置

    需求描述:简单的控制file的选择类型 解决方法:使用HTML  input file 的accept属性控制 实例: <form action="demo_form.asp" ...

  7. python--BOM和DOM

    一. 介绍 什么是BOM和DOM? 简要答案:BOM是浏览器对象模型,用来获取或设置浏览器的属性.行为,例如:新建窗口.获取屏幕分辨率.浏览器版本号等. DOM是文档对象模型,用来获取或设置文档中标签 ...

  8. 打印 Python 的一切 —— pprint & beeprint

    打印,是所有程序员从小白时期就具备的神技,遇事不决打印一下,是 DEBUG 最简单且不依赖 IDE 的方式,自定义各种日志输出,也是项目成型后必备功能.但是为了优雅的打印格式,往往需要对各种对象进行特 ...

  9. python基础学习笔记——内置函数

    一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...

  10. TCP三次握手及四次挥手详解及常见面试题

    https://blog.csdn.net/ZWE7616175/article/details/80432486