My java 0ms solution use own stack may help u


  • 1
    public class Solution {
        public boolean isValid(String s) {
            int len = s.length();
    		char[] stack = new char[len + 1];
    		int top = 1;
    		for (int i = 0; i < len; ++i) {
    			char c = s.charAt(i);
    			if (c == '(' || c == '[' || c == '{')
    				stack[top++] = c;
    			else if (c == ')' && stack[top - 1] != '(')
    				return false;
    			else if (c == ']' && stack[top - 1] != '[')
    				return false;
    			else if (c == '}' && stack[top - 1] != '{')
    				return false;
    			else
    				--top;
    		}
    		if (top == 1)
    			return true;
    		return false;
        }
    }

  • 2
    P

    A good modification would be to set stack size to be (len / 2) + 1
    if you exhaust entire stack and there are more elements then there is no way you will find a matching solution.


  • 0

    I just consider the case that s equals "((((((((". If it appears couple, ur thought is avaliable.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.