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.

https://oj.leetcode.com/problems/longest-valid-parentheses/

分析,求出一串由:‘(’和‘)’组成的字符串中合法()配对的长度。例如:(()(),结果是4。((()))结果是6。())()()结果是4。

解题思路:最容易想到的解法就是穷举法,计算任意两点(i到j)之间有多少个合法的()串,借助动态规划可以得到结果。算法复杂度为:

想要O(n)的解法需要一点技巧,也要弄清楚几种情况:

AC代码如下所示:

public class Solution {
    public int longestValidParentheses(String s) {
        if(s==null||s.length()==0) {
            return 0;
        }
        int start     = -1;
        int maxLength = 0;
        Stack stack   = new Stack();
        for(int i=0;i<s.length();i++) {
            if(s.charAt(i)=='(') {
                stack.push(i);
            } else {
                if(!stack.empty()) {
                    stack.pop();
                    if(stack.empty()==true) {
                        maxLength = Math.max(i - start , maxLength);
                    } else {
                        maxLength = Math.max(i - (int)stack.peek() , maxLength);
                    }
                } else {
                    start = i;
                }
            }
        }

        return maxLength;
    }
}

时间: 2024-10-10 06:20:56

Leetcode Longest Valid Parentheses 结题报告的相关文章

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

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 &

20. Valid Parentheses做题报告

题目链接: Valid Parentheses 题目大意: 判断字符串s的字符是否满足符号匹配 做题报告: (1)该题涉及的算法与数据结构 栈,哈希表 (2)自己的解答思路+代码+分析时间和空间复杂度 思路: 栈先入后出特点,若遇到左括号入栈,遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空则表示满足符号匹配,输出true,否则输出false 代码: import java.util.Stack; class Solution { public boolean isVa