设为首页 加入收藏

TOP

Leetcode Longest Valid Parentheses 结题报告
2015-07-20 17:37:57 来源: 作者: 【 】 浏览:3
Tags: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
  
   


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Boost.Asio c++ 网络编程翻译(6) 下一篇C++(10)这次的C++考试你过了没..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·利用python进行数据 (2025-12-25 20:49:22)
·如何使用 python 中 (2025-12-25 20:49:19)
·零基础如何学爬虫技 (2025-12-25 20:49:17)
·Java 并发工具类:提 (2025-12-25 20:25:44)
·Java面试技巧:如何 (2025-12-25 20:25:41)