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; ...
随机推荐
- LInux在线安装JDK
1.查找Java相关列表: [root@localhost ~]# yum -y list java* 2.使用root用户安装安装时提醒必须使用root用户,sudo都不行. [root@local ...
- Ubuntu 安装配置Jenkins
一.安装jdk 1. sudo add-apt-repository ppa:webupd8team/java 添加ppa源 如果提示找不到该命令则需要安装: sudo apt-get install ...
- linux命令-xz压缩
xz gzip bzip2使用方法基本一样 压缩文件 [root@wangshaojun ~]# xz 111.txt[root@wangshaojun ~]# ls //////111.txt文件 ...
- shell入门-grep-3-egrep
grep -E == egrep [root@wangshaojun ~]# grep --color 'r\?o' 1.txt == egrep --color 'r?o' 1.txt ^C[roo ...
- centos6.5安装tomcat7.0教程(二)
阅读之前对基本命不熟悉的话, 可以先安装另一文章: http://www.cnblogs.com/duenboa/articles/6665159.html把基本的命令记一下.后面的文章就不重复演示了 ...
- SpringMVC 学习笔记(拦截器的配置))
在设置SpringMVC的拦截器时,需要在SpringMVC中配置 拦截器对象,拦截器的的对象要 实现 HandlerInterceptor 接口 拦截器类的设置: public class inte ...
- C基础题-sizeof
sizeof C语言中判断数据类型或者表达式长度符:关键字:字节数的计算在程序编译时进行,而不是在程序执行的过程中才计算出来! 一.关于sizeof简单的总结 1.sizeof的使用形式:sizeo ...
- HTable基本概念
出处:http://www.taobaotest.com/blogs/1582 引言 团队中使用HBase的项目多了起来,对于业务人员而言,通常并不需要从头搭建.维护一套HBase的集群环境,对于其架 ...
- Entity Framework Code-First(1):Introduction
Entity Framework Code-First: Learn Entity Framework Code-First in simple step-by-step tutorials. The ...
- Kolla Ocata版本安装及镜像制作流程
1.关闭宿主机firewalldsystemctl disable firewalldsystemctl stop firewalld 2.配置selinux为disable,否则创建的实例网络不通临 ...