【LeetCode OJ】Valid Parentheses
来源:互联网 发布:怎么才可以在淘宝买烟 编辑:程序博客网 时间:2024/05/12 22:27
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
java code : 典型栈的应用
public class Solution { public boolean isValid(String s) { // Note: The Solution object is instantiated only once and is reused by each test case. if((s.length() & 1) == 1)return false; Stack<Character> st = new Stack<Character>();for(int i = 0; i < s.length(); i++){if(i == 0 || isLeft(s.charAt(i)))st.add(s.charAt(i));else{if(st.isEmpty())return false;if(isMatch(st.peek(), s.charAt(i)))st.pop();else return false;}}return st.isEmpty(); } public boolean isLeft(char ch){if(ch == '(' || ch == '[' || ch == '{')return true;return false;}public boolean isMatch(char x, char y){if((x == '(' && y == ')') || (x == '[' && y == ']') || (x == '{' && y == '}'))return true;return false;}}
- 【LeetCode OJ】Valid Parentheses
- LeetCode OJ:Valid Parentheses
- LeetCode OJ - Valid Parentheses
- LeetCode OJ Valid Parentheses
- [LeetCode OJ]Valid Parentheses
- LeetCode OJ:Longest Valid Parentheses
- LeetCode OJ - Longest Valid Parentheses
- LeetCode OJ Longest Valid Parentheses
- <LeetCode OJ> 20. Valid Parentheses
- 【LeetCode OJ 20】Valid Parentheses
- LeetCode OJ-20-Valid Parentheses
- LeetCode OJ-20.Valid Parentheses
- LeetCode OJ 20 Valid Parentheses [Easy]
- LeetCode OJ算法题(二十):Valid Parentheses
- LeetCode OJ算法题(三十一):Longest Valid Parentheses
- LeetCode OJ 之 Valid Parentheses(有效的括号判断)
- LeetCode OJ 之 Longest Valid Parentheses (最长有效括号)
- LeetCode: Valid Parentheses
- ERROR: NMO not setuid-root (Unix-only)错误的解决方法
- C++不同类中的特征标相同的同名函数
- C++类对象的复制-拷贝构造函数——The c + + class object replication - copy constructor
- 代码编辑器
- hdu-2009-求数列的和
- 【LeetCode OJ】Valid Parentheses
- 矩阵相乘
- ACM-ICPC Live Archive 6139 - Interval Product (线段树-点更新)
- 读coco2d-x里的hellolua
- Objective-C的欢迎度随着iPad和iPhone的衰退而衰减
- 红外摄像头
- 函数的默认参数
- iOS下的unit test——写在测试框架之后
- initramfis的制作以及移植