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;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值