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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true
 class Solution {
public:
bool isValid(string s) {
int l = s.length();
if(l == ) {
return true;
} stack<char> st;
for(int i=; i<l; i++) {
if(s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
}
else if(st.size() == ) {
return false;
}
else {
if(s[i] == ')') {
if(st.top() == '(') {
st.pop();
}
else {
return false;
}
}
else if(s[i] == ']') {
if(st.top() == '[') {
st.pop();
}
else {
return false;
}
}
else {
if(st.top() == '{') {
st.pop();
}
else {
return false;
}
}
}
}
if(st.size() == ) {
return true;
}
else {
return false;
}
}
};

LeetCode - 20. Valid Parentheses(0ms)的更多相关文章

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

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

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

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

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

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

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

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

  5. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  6. leetcode 20 Valid Parentheses 括号匹配

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

  7. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  8. LeetCode 20 -- Valid Parentheses

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

  9. Java [leetcode 20]Valid Parentheses

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

随机推荐

  1. MVVM及框架的双向绑定

    MVVM由以下三个内容组成 View:视图模板 Model:数据模型 ViewModel:作为桥梁负责沟通View和Model,自动渲染模板 在JQuery时期,如果需要刷新UI时,需要先取到对应的D ...

  2. Django-rest-framework(一)简单入门使用

    简单的使用 Django-rest-framework 建成DRF,可以帮助我们快速构建出 django的rest full 风格的api接口. 其源码容易理解,所以我们可以很方便的使用. 安装 pi ...

  3. 原生js的常见封装

    )); } ;;;;]){                 ];                 ] = ;;;,)     ,)     ,)     ,)         ,)         , ...

  4. mybatis where in语句中参数过多

    ps : mybatis在in中参数过多 出现问题 com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数.该服务器支持最多 210 ...

  5. Windows环境下的RTKPlot_Qt版本编译时遇到的问题和解决方法

    在使用了 RTKLIB开源包自带的 rtkplot.exe后,知道了它所具有的功能,就想着如何模仿它做出一个 demo.一开始看的是之前下载的 2.4.2版本的 RTKLIB,里面是使用 Delphi ...

  6. [洛谷]P3704-数字表格

    [洛谷]P3704-数字表格 妙啊,这又是一道反演题,而且个人感觉比较高级 传送门 大意 在\(N\times M\)的数表\(a\)中,\(a_{i,j}\)表示f((i,j)),其中\((i,j) ...

  7. ABAP术语-EDI (Electronic Data Interchange)

    EDI (Electronic Data Interchange) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/29/1057386.ht ...

  8. shardedJedisPool工具类

    这里使用的是ShardedJedisPool,而不是RedisTemplate 1.配置文件 <?xml version="1.0" encoding="UTF-8 ...

  9. Keras模型的保存方式

    Keras模型的保存方式 在运行并且训练出一个模型后获得了模型的结构与许多参数,为了防止再次训练以及需要更好地去使用,我们需要保存当前状态 基本保存方式 h5 # 此处假设model为一个已经训练好的 ...

  10. CentOS7下rsync服务的基本详解和使用

    第1章 Rsync基本概述 1.1 什么是Rsync rsync是一款开源,快速,多功能的可实现增量的本地或远程的数据镜像同步备份的优秀工具.适用于多个平台.从软件名称可以看出来是远程同步的意思(re ...