LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间

题目:

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.

意思是就是找出最长的括号匹配的长度。

这道题也有很多报告,大概看了一点,我看到的多是DP和用栈来做标记,都有用到O(N)的空间复杂度。其实O(N)的空间复杂度是不必要的,提出一个O(N)时间,O(1)空间的解法。

主要思路是正序逆序各遍历一次【正序需要遍历,逆序最差需要遍历】。

  • 首先正向
  1. 像(()))这样会出现)多于(的地方很好做,把前面匹配的长度记录下来就好。
  2. 问题在于((()这样(始终多于)的地方怎么处理,怎么找到这里面符合条件的串。这个时候就需要逆向来解决它,实际就是剔除多余的(。
  • 逆向
  1. 逆向遍历之前的(多于)的串,这串最多只会有一个而且一定是在最后。因为:如果出现了一个这样的串,它就有能力一直往后遍历,即它既能容纳(也能容纳)。
  2. 反过来,从)开始,直接在(多于)的地方即不合格的地方,把前面匹配的长度记录下来。在这个过程中,显然每一个)都会有(来匹配,找出了这个串中符合条件的串。
  • 最后,因为有可能()一样多,也就是结束的时候很完美地匹配了,这个时候的长度也记录一下,返回这个过程中最大的长度。

所谓的记录下来实际上是直接和最大长度比较,更新最大长度。代码如下:

public class Solution {
    public int longestValidParentheses(String s) {
        int left = 0 , len = 0 , tmpl = 0 , pos = -1;

        for( int i = 0; i != s.length(); i++ ){
            if( s.charAt(i) == '(' ){
                left ++; tmpl ++;
            }else{
                left --;
                if( left == -1 ){
                    if( tmpl > len ){
                        len = tmpl;
                    }
                    left = 0; tmpl = 0;
                    pos = i;
                }else{
                    tmpl ++;
                }
            }
        }

        if( left > 0 ){
            left = 0; tmpl = 0;
            for( int i = s.length() - 1 ; i != pos; i-- ){
                if( s.charAt(i) == ')' ){
                    left ++; tmpl ++;
                }else{
                    left --;
                    if( left == -1 ){
                        if( tmpl > len ){
                            len = tmpl;
                        }
                        left = 0;  tmpl = 0;
                    }else{
                        tmpl ++;
                    }
                }
            }
        }
        if( left == 0 ){
            if( tmpl > len ) len = tmpl;
        }

        return len;
    }
}
时间: 2024-10-15 07:02:30

LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间的相关文章

[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 i

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

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 &

[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 &

LeetCode -- Longest Valid Parentheses(Dynamic Programming)

题目地址:https://leetcode.com/problems/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

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 &

[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 &

[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 &

leetcode: Longest Valid Parentheses分析和实现

题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的,因此解题也无从说起.整道题目的难度在于我们对有效括号序列的理解和定义.下面给出我自己的定义:. 定义1:空括号序列是有效的. 定义2:对于一对左右括号,若左括号出现在右括号的左边,且左右括号之间(不包含两端)的括号序列是有效的,那么称该左括号到该右括号(包含)这一段序列是有效的.且称该左括号和右括