zoukankan      html  css  js  c++  java
  • Leetcode-Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

    For "(()", the longest valid parentheses substring is "()", which has length = 2.

    Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

    Analysis:

    At position i, if we get a ')', we find out the last '(', the current maxLen == i-lastPos+1. In addition, if s[lastPos-1] has maxLen[lastPos-1]>0, we need to add it to current maxLen, because the corresponding '(' for current position eliminate the obstacle between current max len sequence and the last max len sequence. For example, "())(())".

    Solution 1:

    We use a stack to store the index of each '('.

     1 public class Solution {
     2     public int longestValidParentheses(String s) {
     3         int n=0,m=0,curLen=0;
     4         List<Integer> pos = new ArrayList<Integer>();
     5         int[] maxLen = new int[s.length()];
     6         int finalRes = 0;
     7 
     8         for (int i=0;i<s.length();i++){
     9             char cur = s.charAt(i);
    10             if (cur=='('){
    11                 pos.add(i);
    12                 maxLen[i]=0;
    13             } else{
    14                 if (pos.size()==0) maxLen[i]=0;
    15                 else {
    16                     int last = pos.get(pos.size()-1);
    17                     pos.remove(pos.size()-1);
    18                     curLen = i-last+1;
    19                     if (last-1>=0) curLen += maxLen[last-1];
    20                     maxLen[i] = curLen;
    21                     if (finalRes<maxLen[i]) finalRes = maxLen[i];
    22                 } 
    23             }                   
    24          }
    25          return finalRes;
    26     }
    27 }

    Solution 2:

    We actually do not need to use a stack. The maxLen of the (i-1) position actually tell us the position of the last '('.

     1 public class Solution {
     2     public int longestValidParentheses(String s) {
     3         if (s.length()==0) return 0;
     4         int n=0,m=0,curLen=0;       
     5         int[] maxLen = new int[s.length()];
     6         int finalRes = 0;
     7         maxLen[0] = 0;
     8         for (int i=1;i<s.length();i++){
     9             char cur = s.charAt(i);
    10             if (cur=='('){               
    11                 maxLen[i]=0;
    12             } else{
    13                 int lastLen = maxLen[i-1];
    14                 int lastIndex = i-1-lastLen;
    15                 
    16                 if (lastIndex>=0 && s.charAt(lastIndex)=='('){
    17                     maxLen[i] = lastLen+2;
    18                     if (lastIndex-1>=0) maxLen[i] += maxLen[lastIndex-1];
    19                     if (finalRes<maxLen[i]) finalRes = maxLen[i];
    20                 }
    21             }                   
    22          }
    23          return finalRes;
    24     }
    25 }
  • 相关阅读:
    谎言,
    happy,
    架构,
    休闲游戏随想,
    IOS响应者链
    application 几个方法
    ios block 循环引用
    洛谷 P 1133 教主的花园
    Codevs 1148 == 洛谷 P1057 传球游戏
    Codevs 1169 == 洛谷 P1006 传纸条
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4112575.html
Copyright © 2011-2022 走看看