利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是否与栈顶匹配,若不匹配则false。

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

LeetCode 20. Valid Parentheses(c++)的更多相关文章

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

  10. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. codeforces 796A-D

    决定在 codeforces 练题啦,决定每个比赛刷前四道...太难就算了 796A Buying A House 题意:给出x轴上的n 个点,每个点有个权值,问离m 点最近的权值小于等于k 的点离m ...

  2. Commons-DbUtils

    <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils& ...

  3. delphi7 编译的程序在win7下请求获得管理员权限的方法

    网上找到的,记下来方便查找,亲测此方法可用.附带把编译好的uac.res上传. 首先,用记事本新建一文本文档,内容如下: 1 24 UAC.manifest 然后另存为uac.rc 另外新建一文本档, ...

  4. table 里输入rules 验证

    HTML <el-form ref='from' :model="fromData"> <el-table ref="tableman" bo ...

  5. Elasticsearch 目录总结

    一:Elasticsearch (及工具插件)安装相关: 二:Elasticsearch 数据新增相关: 三:Elasticsearch 数据删除相关: 四:Elasticsearch 数据更新相关: ...

  6. 快速傅里叶变换FFT& 数论变换NTT

    相关知识 时间域上的函数f(t)经过傅里叶变换(Fourier Transform)变成频率域上的F(w),也就是用一些不同频率正弦曲线的加 权叠加得到时间域上的信号. \[ F(\omega)=\m ...

  7. spring问题

    1.The matching wildcard is strict ,but no declaration can be found for element 'tx:annotation-driven ...

  8. (双指针) leetcode 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  9. 2018-2019 2 20175230《Java程序设计》第九周学习总结

    <Java程序设计>第九周学习总结 主要内容 MySQL数据库管理系统 1.下载 2.安装 启动MySQL数据库服务器 1.启动 2.root用户 MySQL客户端管理工具 建立连接 建立 ...

  10. (八) Usb摄像头描述符解析

    目录 Usb摄像头描述符解析 总结 参考资料 打印设备描述符 打印配置描述符 打印接口联合体描述符 打印接口描述符 打印当前设置的额外描述符 代码解析额外的描述符 打印端点描述符 title: Usb ...