题目链接

Valid Parentheses - LeetCode

注意点

  • 考虑输入为空的情况

解法

解法一:如果是'('、'{'、'['这三者就入栈,否则就判断栈是否为空和栈顶括号是否与之匹配。注意两个判断顺序不可以颠倒,不然会runtime error。时间复杂度为O(n)

class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto &ch:s)
{
if(ch == '('||ch == '{'||ch == '[')
{
stk.push(ch);
}
else if(ch == ')')
{
if(stk.empty()||stk.top() != '(')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == '}')
{
if(stk.empty()||stk.top() != '{')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == ']')
{
if(stk.empty()||stk.top() != '[')
{
return false;
}
else
{
stk.pop();
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
};

小结

一些栈的基本操作:

  • push(): 向栈内压入一个成员;
  • pop(): 从栈顶弹出一个成员;(注意要判断栈是否为空)
  • empty(): 如果栈为空返回true,否则返回false;
  • top(): 返回栈顶,但不删除成员;(注意要判断栈是否为空)
  • size(): 返回栈内元素的大小

Valid Parentheses - LeetCode的更多相关文章

  1. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  2. Longest Valid Parentheses leetcode java

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

  3. Longest Valid Parentheses - LeetCode

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

  4. Valid Parentheses leetcode java

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

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

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

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

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

  7. [LeetCode] Valid Parentheses 验证括号

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

  8. Java for LeetCode 032 Longest Valid Parentheses

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

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

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

随机推荐

  1. springboot+elasticsearch实现全局检索

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  2. 使用maven&&make-distribution.sh编译打包spark源码

    1>基础环境准备: jdk1.8.0_101 maven 3.3.9scala2.11.8 安装好上述软件,配置好环境变量,并检查是否生效. 2>配置maven:intellij idea ...

  3. 比较语义分割的几种结构:FCN,UNET,SegNet,PSPNet和Deeplab

    简介 语义分割:给图像的每个像素点标注类别.通常认为这个类别与邻近像素类别有关,同时也和这个像素点归属的整体类别有关.利用图像分类的网络结构,可以利用不同层次的特征向量来满足判定需求.现有算法的主要区 ...

  4. linux 安装配置zookeeper脚本

    #!/bin/bash # automatic install zookeeper echo "========= Start to install zookeeper ========== ...

  5. Vue 列表渲染及条件渲染实战

    条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue 帮我们提供了一个v-if的指 ...

  6. alpha版postmortem 报告

    一.团队开发存在的问题 此次会议我们团队中每个成员都仔细思考并提出了团队在这一阶段存在的问题,主要如下: 1.前期任务规划.分配不合适: 2.个人对认领任务模块完成度.了解度不够: 3.个人学习意识. ...

  7. TCP/IP,HTTP,HTTPS,WEBSocket协议

    我看看着挺多的,我暂时没时间自己写,有需要的请借鉴 http://mp.weixin.qq.com/s?__biz=MzI0MDQ4MTM5NQ==&mid=2247486927&id ...

  8. JS学习:JavaScript的核心

    分享到 分类 JS学习   发布 ourjs  2013-12-02 注意 转载须保留原文链接,译文链接,作者译者等信息.     作者: JeremyWei  原文: JavaScript The ...

  9. Sprint Boot入门(1):创建第一个Spring Boot应用

    搭建工程 注:建议使用eclipse的STS插件创建Spring项目,而不是下面的Gradle项目,否则会导致有一些Spring文件不存在. new Gradle Project,如下 点next,如 ...

  10. matlab的应用

    MATLAB在信号与系统中的应用 一: 看到MATLAB在信号与系统中的应用,对这部分我比较熟悉,在此举一个小例子.    impulse(sys)表示求连续系统sys的冲击响应    subplot ...