020 Valid Parentheses 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
括号必须以正确的顺序关闭,"()" 和 "()[]{}" 是有效的但是 "(]" 和 "([)]" 不是。
详见:https://leetcode.com/problems/valid-parentheses/description/
实现语言:Java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
Character now;
Character prev;
int length=s.length();
for(int i=0;i<length;i++){
now=s.charAt(i);
if(now=='('||now=='['||now=='{'){
stack.push(now);
}else{
if(stack.isEmpty()){
return false;
}else{
prev=stack.pop();
}
if(now==')'&&prev!='('){
return false;
}else if(now==']'&&prev!='['){
return false;
}else if(now=='}'&&prev!='{'){
return false;
}
}
}
return stack.isEmpty();
}
}
参考:https://blog.csdn.net/runningtortoises/article/details/45621933
http://www.cnblogs.com/grandyang/p/4424587.html
020 Valid Parentheses 有效的括号的更多相关文章
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- LeetCode 020 Valid Parentheses
题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...
- LeetCode OJ: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】020. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【JAVA、C++】LeetCode 020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode][020] Valid Parentheses (Java)
题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
随机推荐
- Poj 2602 Superlong sums(大数相加)
一.Description The creators of a new programming language D++ have found out that whatever limit for ...
- kvm 基础 虚拟机改名
转自:http://www.cnblogs.com/5201351/p/4464350.htm 1.查看所有的kvm虚拟机 [root@5201351_kvm ~]# virsh list --all ...
- 在Action获取Scope对象
引言:在前面的Action操作中,关键就是Action中的exectue方法,但是此方法并没有request.session.application等对象作为参数,自然就不能利用这些对象来操作.下面我 ...
- [matlab]bp神经网络工具箱学习笔记
基本就三个函数: newff():创建一个bp神经网络 train():训练函数 sim():仿真函数 同时具有可视化界面,但目前不知道可视化界面如何进行仿真,且设置不太全 工具箱:Neural ne ...
- mvn jetty:run 出现PermGen space outofmemeryerror
使用mvn jetty:run跑别人的项目时出现了PermGen space outofmemeryerror异常 http://stackoverflow.com/questions/1451648 ...
- hbase-0.98.1-cdh5.1.0伪分布式安装
分三步: 1. 添加环境变量 2.编辑hbase-env.sh文件 3.编辑hbase-site.xml 文件 前提条件是安装好hadoop,下面展开说明 1. 添加环境变量 export HBASE ...
- java连接sqlserver2005数据库
java连接sqlserver2005数据库 首先得下载驱动程序到微软网站下载Microsoft JDBC Driver 4.0 for SQL Server 下载地址 :http://msdn. ...
- 各版本Google浏览器下载地址
各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/
- ibatis分页的两种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 诺基亚 920T - 我的非凡系列手机始终显示旋转齿轮而无响应,我该如何让手机停止显示旋转齿轮?
有时,在 OTA 更新 (或重置手机) 后,设备可能始终显示“旋转齿轮”而无响应. 如果“旋转齿轮”在屏幕上显示的时间超过 60 分钟,则需要执行恢复操作. 您可以尝试下面这些简单的解决方法: 按住电 ...