Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 30, 2014 15:27
Show Gist options
  • Save Cee/984730ac121bcf580ab9 to your computer and use it in GitHub Desktop.
Save Cee/984730ac121bcf580ab9 to your computer and use it in GitHub Desktop.
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
if (s.length() % 2 != 0) return false;
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if ((c == '(') || (c == '[') || (c == '{')){
stack.push(c);
}else{
if (stack.empty()){
return false;
}else{
char p = stack.pop();
if (!( ((p == '(') && (c == ')'))
|| ((p == '[') && (c == ']'))
|| ((p == '{') && (c == '}'))))
return false;
}
}
}
if (stack.empty()){
return true;
}else{
return false;
}
}
}
@Cee
Copy link
Author

Cee commented May 30, 2014

About stack
注意所有Collection类型里面要用Character或者Integer封装为对象。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment