您的位置:首页 > 其它

Valid Parentheses

2016-06-26 08:44 375 查看
public class Solution {
public boolean isValid(String s) {
if (s == null || s.length() % 2 == 1) {
return false;
}
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '{' || c == '[' || c == '(') {
stack.push(c);
continue;
}
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((top == '{' && c == '}')|| (top == '[' && c == ']') || (top == '(' && c == ')')) {
continue;
} else {
return false;
}
}
if (stack.isEmpty()) {
return true;
} else {
return false;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: