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. HDU 1111 Secret Code(数论的dfs)

    Secret Code Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  2. js读取跨域webapi传送回来的cookie 要点

    1.webapi 返回cookie时,httpOnly=false 2.webapi 接收Origins 不能为* 3.js端 请求时,withCredentials必须: true   //`wit ...

  3. 表达式过滤器 uppercase

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  4. 路由器基本配置实验,静态路由和动态RIP路由

    实验涉及命令以及知识补充 连线 PC和交换机FastEtherNet接口 交换机和路由器FastEtherNet接口 路由器和路由器Serial接口 serial是串行口,一般用于连接设备,不能连接电 ...

  5. 开发一个c#的数据库连接池

    c#操作数据库是一个经典,用习惯了以后真感觉不错,很简单的.现在很多关系数据库都支持c#.c#的ADO.NET规范都遵守. 对于一般的设置,ADO.NET都放在数据库连接字符串上.比如池化,连接超时等 ...

  6. swap, 不用临时变量如何做到交换a与b

    固定思维通常是需要一个临时变量temp,如果没有这个临时变量呢,其实也不复杂,:) inline void swap(int &a, int &b) /*C用指针吧*/ { a = a ...

  7. POJ 2398--Toy Storage(叉积判断,二分找点,点排序)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6534   Accepted: 3905 Descr ...

  8. VM虚拟机网卡LAN区段模拟内网使用教程

    目录   1. 测试环境   2. 设置LAN区段并测试    2.1. 添加LAN区段    2.2. 在虚拟机中设置静态IP地址    2.3. 测试同一LAN区段的主机是否可以联通    2.4 ...

  9. java的动态验证码单线设计

    1.java的动态验证码我这里将介绍两种方法: 一:根据java本身提供的一种验证码的写法,这种呢只限于大家了解就可以了,因为java自带的模式编写的在实际开发中是没有意义的,所以只供学习一下就可以了 ...

  10. 【ssh服务配置】

    根据项目需求,搭建好拓扑图如下: 第一种验证方式:给予密码和用户名登录 Ssh server配置: 首先在服务器上创建一个rsa加密算法的秘钥对: 对ssh服务进行开启: 创建用户的虚拟终端登录界面: ...