标签:leetcode解题报告 面试题 算法 java
Given a string containing just the characters ‘(‘ and ‘)‘, find the length of the longest valid (well-formed) parentheses substring.public class Solution { public int longestValidParentheses(String s) { if(s==null||s.length()==0) { return 0; } int start = -1; int maxLength = 0; Stack stack = new Stack(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='(') { stack.push(i); } else { if(!stack.empty()) { stack.pop(); if(stack.empty()==true) { maxLength = Math.max(i - start , maxLength); } else { maxLength = Math.max(i - (int)stack.peek() , maxLength); } } else { start = i; } } } return maxLength; } }
Leetcode Longest Valid Parentheses 结题报告
标签:leetcode解题报告 面试题 算法 java
原文地址:http://blog.csdn.net/worldwindjp/article/details/39460161