leetcode 032 Longest Valid Parentheses

35 篇文章 0 订阅
35 篇文章 0 订阅

题目

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.

思路:

利用栈和标记数组来做。

  1. 初始化:

    • 标记数组flags,类型为boolean,长度为输入字符串的长度,默认全为false,表示开始全未配对;
    • 模拟栈的数组stack,类型为int,长度为输入数组的长度。
  2. 遍历输入字符串:

    • 如当前字符为’(‘,则在stack中记录其下标
    • 若当前字符为’)’:判断stack是否为空,不为空则说明前面有与之配对的左括号 ,则将这两个括号所对应的flags全标为true,表示已配对;否则什么都不做,继续遍历。
  3. 当遍历结束后,标记数组中已配对的括号位置都被设为了true。比如:假设输入字符串为")())()()“,则flags为{false,true,true,false,true,true,true,true};

  4. 从flags中找出连续为true的最大长度,即为所求最大配对长度。

代码:

public int longestValidParentheses(String s)
{
    int length = s.length();
    boolean[] flags = new boolean[length];  //用来标记是否配对
    int[] stack = new int[length]; //模拟栈,用来记录未配对的括号的位置
    int top = 0;    //栈顶指针
    for(int i = 0; i < length; i++)
    {
        if(s.charAt(i) == ')')
        {
            //如果当前为')'话,且栈不为空,则这个右括号之前有与之配对的左括号
            //将这两个括号对应的标志位都设为true,表明已配对,同时栈顶指针减1
            if(top > 0)
                flags[i] = flags[stack[--top]] = true;
        } else
        {
            //如果当前为'(',则在栈中记录其位置
            stack[top++] = i;
        }
    }

    //循环结束后,标记数组中已配对的括号位置都被设为了true
    //比如:假设输入字符串为")())()()",则flags为{false,true,true,false,true,true,true,true};
    //下面只需从flags中找到连续为true的最大长度即可,此长度就是最大配对长度
    int res, tmp;
    res = tmp = 0;
    int i;
    for(i = 0; i < length; i++)
    {
        if(flags[i])
            tmp++;
        else
        {
            res = tmp > res ? tmp : res;
            tmp = 0;
        }
    }
    return (res < tmp) ? tmp : res;
}

结果细节(图):

image

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值