1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Solution { public boolean isValid(String s) { Stack<Character> lookup = new Stack<Character>(); for (int i = 0; i < s.length(); i++){ char temp = s.charAt(i); if (isFaceRight(temp)) lookup.push(temp); else{ if (lookup.isEmpty() || lookup.peek() != match(temp)) return false; lookup.pop(); } } return lookup.isEmpty(); } private boolean isFaceRight(char a){ return a == '(' || a == '{' || a == '['; } private char match(char a){ if (a == ')') return '('; if (a == '}') return '{'; return '['; } } |
Friday, November 27, 2015
[leetcode]Valid Parentheses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment