博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Valid Parentheses -- LeetCode
阅读量:4583 次
发布时间:2019-06-09

本文共 941 字,大约阅读时间需要 3 分钟。

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

思路:用栈。要注意的一点是,当读到了右括号时,在判断栈顶是否是对应的左括号前,要先确定栈是否为空。

1 class Solution { 2 public: 3     bool isValid(string s) { 4         stack
va; 5 for (auto i : s) 6 { 7 if (i == '(' || i == '{
' || i == '[') 8 va.push(i); 9 else if (va.empty()) return false;10 else if (i == ')' && va.top() != '(')11 return false;12 else if (i == '}' && va.top() != '{
')13 return false;14 else if (i == ']' && va.top() != '[')15 return false;16 else va.pop();17 }18 return va.empty();19 }20 };

 

转载于:https://www.cnblogs.com/fenshen371/p/5162809.html

你可能感兴趣的文章
elasticsearch摸石头过河——常用数据类型(二)
查看>>
scrum立会报告+燃尽图(第三周第三次)
查看>>
[SQL] 获取 Microsoft SQL Server 2008 的数据表结构
查看>>
iOS进度指示器——NSProgress
查看>>
C语言strcat,ctrcpy函数原型和改进
查看>>
good bye 2015 B - New Year and Old Property
查看>>
(第4篇)hadoop之魂--mapreduce计算框架,让收集的数据产生价值
查看>>
万年历-农历-农历日期
查看>>
如何辞职
查看>>
SSO 单点登录总结(PHP)
查看>>
Ubuntu16.04下将hadoop2.7.3源代码导入到eclipse neon中
查看>>
朝令夕改的企业不值得留恋
查看>>
springboot踩坑出坑记
查看>>
ovs源码阅读--netlink使用
查看>>
php中引用&的真正理解-变量引用、函数引用、对象引用
查看>>
cmake编译安装mysql 5.6.12
查看>>
第七章学习小结
查看>>
GS LiveMgr心跳管理类
查看>>
设计模式学习笔记(二)之观察者模式、装饰者模式
查看>>
mysql导出数据库和恢复数据库代码
查看>>