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; ...
随机推荐
- 事务之五:Spring @Transactional工作原理
本文将深入研究Spring的事务管理.主要介绍@Transactional在底层是如何工作的. JPA(Java Persistence API--java持久层)和事务管理 很重要的一点是JPA本身 ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...
- C# 利用委托和事件 传入一个参数进行进行计算并返回结果
一.委托定义 1: public class TestData 2: { 3: //定义委托 4: public delegate void Get_TestDataEventHandler(Get_ ...
- Java探索之旅(15)——包装类和字符类
1.包装类 ❶出于对性能的考虑,并不把基本数据类型作为对象使用,因为适用对象需要额外的系统花销.但是某些Java方法,需要对象作为参数,例如数组线性表ArrayList.add(Object).Jav ...
- spring+jax 出现java.io.Serializable is an interface, and JAXB can't handle interfaces
spring+jax 出现java.io.Serializable is an interface, and JAXB can't handle interfaces 原因是我的webservice方 ...
- 数据库导入sql文件
数据库导入sql文件包括两种方式: 一.命令行导入 二.数据库工具导入 后续完善
- R-安装、卸载、查看及卸除加载包
1. 安装包 install.packages("BiocInstaller") 2. 卸载已安装包 remove.packages("BiocInstaller&quo ...
- iperf3
1.安装 将下载得到的“iperf-3.1.3-win64.zip”文件解压缩,得到“iperf3.exe”和“cygwin1.dll”两个文件.将这两个文件复制到“%systemroot%”(大多数 ...
- linux中制作动态库
制作一个动态库我们可以使用gcc工具来制作一个动态库示例:自己制作一个动态库,库函数的功能是传递一个字符串并输出.第一步:需要准备3个文件:hello.h.hello.c.test.c.其中hello ...
- 【Qt文档阅读】事件系统
在Qt中,事件对象都继承于QEvent类,它表示应用程序内部或由于应用程序需要了解的外部活动而发生的事情.事件可以由QObject子类的任何实例接收和处理,尤其是widget.本文档描述如何在典型应用 ...