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 }
  • 相关阅读:
    空间轴向对齐变换
    购买服务器,搭建服务器服务器
    软件工程第四次作业:猫狗大战挑战赛
    软件工程第三次作业:卷积神经网络
    04卷积神经网络
    03深度学习的数学基础
    mfc回显信息
    软件工程第二次作业:深度学习和pytorch基础
    python 机器学习第二章(感知器学习算法)
    python 机器学习第一章
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4112575.html
Copyright © 2011-2022 走看看