[leetcode] 20. Valid Parentheses (easy)
原题链接
匹配括号
思路:
用栈,遍历过程中,匹配的成对出栈;结束后,栈空则对,栈非空则错。
Runtime: 4 ms, faster than 99.94% of Java
class Solution {
public boolean isValid(String s) {
Stack<Character> sta = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char temp = s.charAt(i);
if (temp == '[' || temp == '(' || temp == '{') {
sta.push(temp);
} else {
if (sta.isEmpty())
return false;
char top = ' ';
if (temp == ']')
top = '[';
if (temp == ')')
top = '(';
if (temp == '}')
top = '{';
if (top == sta.peek())
sta.pop();
else
return false;
}
}
return sta.isEmpty() ? true : false;
}
}
[leetcode] 20. Valid Parentheses (easy)的更多相关文章
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- IntelliJ IDEA的jsp中内置对象方法无法被解析的解决办法
主要原因是因为缺乏依赖 可以通过添加依赖的方式 导入servlet-api.jar,jsp-api.jar,tomcat-api.jar 这三个jar即可 这三个jar在tomcat的lib目录下有 ...
- Qt的QWaitCondition(允许线程在一定条件下唤醒其他线程,这样对不间断上传可能比较适用)
对生产者和消费者问题的另一个解决办法是使用QWaitCondition,它允许线程在一定条件下唤醒其他线程.其中wakeOne()函数在条件满足时随机唤醒一个等待线程,而wakeAll()函数则在条件 ...
- elasticsearch local debug环境搭建
最近计划看看elasticsearch的源码,首先得把local debug环境搞定. 下载源码.因为公司产线是5.6.5,所以就下载了5.6.5的代码. 源码编译.先进入到/elasticsearc ...
- Qt设置窗体的透明度: setWindowOpacity
在Qt中,设置窗体透明度的函数有:void setWindowOpacity(qreal level) 特性: 透明度的有效范围从1.0(完全不透明)到0.0(完全透明的). 默认情况下,此属 ...
- QT 文件拖放事件dropEvent和dragEnterEvent
重载以下两个函数,可以实现将文本文件拖放进文本编辑器 void MainWindow::dragEnterEvent(QDragEnterEvent *event)//拖进事件 { if(event- ...
- SPOJ1421_Goods_循环节
题意:1-n的一个排列,两两互换,每个位置每天只能做一次交换,问最多几天能交换成1-n,并且输出交换步骤. 解法:把该置换中所有的循环节找出,各循环节之间的交换是并行的,两两不相关,每天只需在循环节内 ...
- Windows服务器下的IIS和Apache性能比较
目前最流行的建立网站的服务工具就要属Apache与IIS了.那么他们之间到底哪个性能更好呢?到底哪个工具才是最适合我们的呢?最近我也对这方面的问题进行了一番研究. 如果是基于Linux平台的话,那不必 ...
- java设计模式-原型(prototype)
有时候创建对象是需要耗费很多资源,但是每个对象之间又有大量的重复.我们可以选择在创建好一个对象后,以之作为模板克隆出其他对象,稍作修改,即可用于其他地方. 需要实现Cloneable接口,重写clon ...
- Linux实战型企业运维工程师试题
1.如何通过Linux配置一个局域网或者IDC机房上网网关,请给出步骤及命令?答:上网网关配置(1)开启内核转发:sed -i 's#net.ipv4.ip_forward = 0#net.ipv4. ...
- react-redux的Provider和Connect的引发的思考
react是当下非常流行的JS框架,react秉承的设计原则是一切皆组件:react-redux是react中使用redux的桥接工具,react-redux也继承react的设计原则,使用组件的形式 ...