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

class Solution {
public:bool isValid(string s) {stack<char> c;for(int i=0;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;}
};

转载于:https://www.cnblogs.com/y1040511302/p/10786203.html

LeetCode 20. Valid Parentheses(c++)相关推荐

  1. LeetCode: 20. Valid Parentheses

    0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...

  2. [LeetCode]--20. Valid Parentheses

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

  3. LeetCode 20 Valid Parentheses (C++)

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

  4. [swift] LeetCode 20. Valid Parentheses

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

  5. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  6. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  7. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

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

  8. Leetcode: Longest Valid Parentheses

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

  9. LeetCode 20. Valid Parenthese

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

最新文章

  1. Java黑皮书课后题第7章:**7.24(仿真:优惠券收集问题)优惠券收集问题是一个经典的统计问题。编写程序,模拟要得到四张不同花色的牌所需要的选取次数,然后显示选中的四张牌
  2. 定时器驱动数码管c语言程序,74hc595驱动数码管时间程序
  3. 九零后女孩币圈变形记
  4. 光栅图形学——直线段的扫描转换算法
  5. 大学计算机python考试题二叉树_leetcode 二叉树相关习题总结
  6. PHP 微信小程序支付实现
  7. BAT代码表白实用详细操作
  8. 求职时,怎样判断一家公司是否靠谱?
  9. 苹果华为齐发力AI 人工智能手机时代来了
  10. 找出集合中重复元素和不重复元素
  11. 小米生态企业强力推荐的开源免费SRM采购管理平台功能介绍
  12. 线程安全、线程同步、线程间通信
  13. 论文笔记-Vanilla Transformer:Character-Level Language Modeling with Deeper Self-Attention
  14. JAVA面向对象三大特征之一多态
  15. Python每日一练(13)-IQ智商判断及测试
  16. 数字电路基础(五)算术运算电路
  17. 系统规划---方案的制订和改进
  18. GPU的并行运算与CUDA的简介
  19. 操作系统-进程死锁:银行家算法
  20. 可视化图表入门教程讲解

热门文章

  1. 学生网页设计代码成品 大学生汽车网页代做 红旗汽车网页设计模板下载 HTML汽车网站制作 dreamweaver学生汽车网页设计成品
  2. dedecms二次开发常用代码
  3. matlibplot 一张图画多个曲线_热重法实验条件设定与曲线解析 第53部分 热重实验方案设计与曲线解析常见问题之热重仪选择中常见问题分析...
  4. 信息安全工程师笔记-操作系统安全保护
  5. Flask笔记-使用flask-sqlacodegen自动生成model
  6. C++工作笔记-map中结构体的比较
  7. c语言去空格换行符,关于文件操作,碰到空格就换行
  8. python腐蚀膨胀代码_OpenCV+python实现膨胀和腐蚀的示例
  9. 鸿蒙系统被泼冷水,给鸿蒙泼冷水:见不得同行的好,是人间最可恶的蠢和恶
  10. java jdk最新版本是多少_Linux下一键安装java-jdk任意版本