Created
May 30, 2014 15:27
-
-
Save Cee/984730ac121bcf580ab9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About stack
注意所有Collection类型里面要用Character或者Integer封装为对象。