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 }
  • 相关阅读:
    关于数据库的压测(window+liunx)
    参数化关联----三种方式
    jmeter的使用
    day07----------移动端测试
    day07----------Charles抓取web端的HTTPS协议下载和七个功能操作
    CURL 运用
    有关数据库的导出导入备份
    同时删除多张表的数据
    一条sql语句update 多条记录
    linux 将一个服务器上的文件或文件夹拷贝到另一个服务器上(转载)
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4112575.html
Copyright © 2011-2022 走看看