思路:读取字符串中的每一个字符,并与栈顶元素进行比较,如果匹配则栈顶元素出栈,否则进栈。直到全部元素都出栈,否则该括号字符串不合法。需要注意细节。

     public boolean isValid(String s) {
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<s.length();i++){
char t=s.charAt(i);
if(t=='(' || t=='[' || t=='{'){
stack.push(t);
}else if(!stack.isEmpty()&&(t==')'&&stack.peek()=='(' || t==']'&&stack.peek()=='[' || t=='}'&&stack.peek()=='{')){
stack.pop();
}else{
return false;
}
}
if(stack.isEmpty()){
return true;
}else{
return false;
}
}

【Leetcode-easy】Valid Parentheses的更多相关文章

  1. 【leetcode系列】Valid Parentheses

    非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class Stack ...

  2. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  3. 【Leetcode】【Easy】Valid Parentheses

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

  4. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  5. 【LeetCode练习题】Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  6. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  7. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  8. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  9. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  10. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

随机推荐

  1. 2016.7.14 generator基于注解和基于xml自动生成代码的区别

    1.generator配置文件generatorConfig.xml的区别 2.生成代码的区别 注:二者的实体类都一样. (1)基于XML 生成的文件有: 后面省略. 也就是说,基于xml的方式,是要 ...

  2. hibernate映射排序

    @OneToMany(mappedBy="member") @OrderBy(value = "TousuID desc")

  3. mysql MHA报错 Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 99.

    如果发现如下错误: Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MH ...

  4. SQL Server排序的时候使null值排在最后

    首先建一个表插入一些测试数据 create table UserInfo ( UserInfoID        int not null identity(1,1) primary key, Use ...

  5. Msfvenom 学习笔记与总结

    平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...

  6. python 安装protobuf

    安装准备:python和protoc(编译proto到各个语言) 下载protobuf源代码(各种语言实现):https://github.com/google/protobuf  1.到Python ...

  7. Erlang的系统限制

    Erlang本身对进程数,原子长度等没有限制,但为了提高性能和节省内存,总会在实际实现中和运行环境中作出一些限制. 1.进程数量 缺省情况下同时存在的最大Erlang进程数量不超过2^18=26214 ...

  8. 13-redis主从复制

    一:redis主从复制 (1)配置要求 一台主maste 二台从slave (2)架构图 (3)配置流程 1:分配拷贝2份redis.conf 分别命名为 redis6380.conf redis63 ...

  9. CSS -- 未解之疑

    @.css那些事儿 -- 第9章 反馈表单 自己编写了CSS,可是红框中的横线比下面的要粗.对比作者的代码,发现可能与上面标题h3的height和line-height有关,但是不知道具体是为什么? ...

  10. 最简单的PHP开发环境搭建

    近期发现一个非常easy的,适合刚開始学习的人的PHP开发环境,整个环境仅仅有三样东东,PHP ,APACHE , MYSQL可是对于初学PHP的人来说,己经足够了. 假设有兴趣的话能够直接去百度PN ...