LeetCode 20 Valid Parentheses (括号匹配问题)
package leetcode_50; import java.util.Stack; /***
*
* @author pengfei_zheng
* 括号匹配问题
*/
public class Solution20 {
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
int len = s.length();
for(int i=0;i<len;i++){
char c = s.charAt(i);
if(c=='(' || c=='{' || c=='[') stack.push(c);
if(c==')'){
if(stack.isEmpty())
return false;
else if('('!=stack.pop())
return false;
}
if(c=='}'){
if(stack.isEmpty())
return false;
else if('{'!=stack.pop())
return false;
}
if(c==']'){
if(stack.isEmpty())
return false;
else if('['!=stack.pop())
return false;
}
}
if(!stack.isEmpty())
return false;
else
return true;
}
public static void main(String[]args){
String s="[[(])]";
System.out.println(isValid(s));
}
}
参考代码2:
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
LeetCode 20 Valid Parentheses (括号匹配问题)的更多相关文章
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- 20. Valid Parentheses(括号匹配,用桟)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 20. Valid Parentheses - 括号匹配验证
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- 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有效的括号
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 ...
随机推荐
- C# 判断网站是不是discuz论坛
if (this.txturl.Text == "") { this.lblmess.Text = "请输入网址"; } else { GetHttp getH ...
- Java学习路线图(如何快速学Java)
不知不觉从初学Java到现在已经8年了,今天在这里给刚入门和入门不久的小伙伴们一些建议.可能总结的不是很详细,但给出了一个大概的学习路线.希望对大家有帮助哈~ 如何快速学Java 这里我以Java E ...
- php解析出带层级关系的mpp文件
本来要使用DHX gantt插件自带的API做导入,可是做完后,又发现不稳定,不能访问了 可能是屏蔽掉了 所以又想起可以使用javaBridge,借用java的MPXJ php解析mpp的 上一篇介绍 ...
- Tensorflow物体检测(Object Detection)API的使用
Tensorflow在更新1.2版本之后多了很多新功能,其中放出了很多用tf框架写的深度网络结构(看这里),大大降低了吾等调包侠的开发难度,无论是fine-tuning还是该网络结构都方便了不少.这里 ...
- [原]单片机/Stm32教程
1 http://www.amobbs.com/forum.php?mod=viewthread&tid=4462962 2.http://bbs.21ic.com/forum.php?mod ...
- AWS系列-创建 IAM 用户
创建 IAM 用户(控制台) 官方文档 https://docs.aws.amazon.com/zh_cn/IAM/latest/UserGuide/introduction.html 通过 AWS ...
- Dubbo原码解析(version:2.5.3)
一.启动dubbo借助spring的schemas来启动(dubbo.jar/META-INF/spring.schemas).在dubbo.jar/META-INF/spring.handlers里 ...
- IIS------如何占用80端口
如何占用80端口 请看我的一篇随笔: https://www.cnblogs.com/tianhengblogs/p/9292347.html
- Java使用ListIterator逆序ArrayList
对于列表而言,除了Iterator,还提供了一个功能更加强大的ListIterator.它可以实现逆序遍历列表中的元素.本示例将使用其逆序遍历ArrayList. 思路分析:要逆序遍历某个列表,首先要 ...
- joda time, jackson 与 scala 反射
1. scala 反射,获得所有 field name 可以直接从 case class 获得 field 而不必创建实例 (get fields of a class without an inst ...