My java solution


  • 0
    M
    public int longestValidParentheses(String s) {
            Stack<Integer> stack = new Stack();
    	int length = s.length();
    	boolean []end = new boolean[length];
    	for(int i = 0 ; i < length ; i ++)
    	{
    	        if(s.charAt(i) == '(')
    		    stack.push(i);
    	        else if(!stack.isEmpty())
    		    end[stack.pop()] = end[i] = true; 
    	}
    	int nowsum = 0,maxsum = 0;
    	for(int i = 0 ; i < length ; i ++)
    	{
    	    if(end[i] == false)
    		    nowsum = 0;
    	    else{
    		    nowsum ++;
    		    if(nowsum > maxsum)
    			    maxsum = nowsum;
    	    }
    	}
    	return maxsum;
    }
    

Log in to reply
 

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