第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n)

  1. // 使用栈,时间复杂度 O(n),空间复杂度 O(n)
  2. class Solution {
  3. public:
  4. int longestValidParentheses(string s) {
  5. int max_len = , last = -;
  6. stack<int> lefts;
  7. for (int i = ; i < s.size(); ++i) {
  8. if (s[i] =='(') {
  9. lefts.push(i);
  10. } else {
  11. if (lefts.empty()) {
  12. // update last
  13. last = i;
  14. } else {
  15. // find a matching pair
  16. lefts.pop();
  17. if (lefts.empty()) {
  18. max_len = max(max_len, i-last);
  19. } else {
  20. max_len = max(max_len, i-lefts.top()); }
  21. }
  22. }
  23. }
  24. return max_len;
  25. }
  26. };

第二种方方法,搞一个计数器,每次遇到'('加一,遇到‘)’ 减少一,所以只有在计数器==0的时候更新 max_len。为了实现功能,必须两侧扫描,从而在计数器==0的时候更新max_len.

例如( ( () () 由于左括号多,因此从左侧扫描无法达到计数器==0,所以,max_len还是0.但当从右侧扫描是,右括号较少,会达到0,从而得到真实的max_len.保存start的思想和栈方法一致。

  1. // LeetCode, Longest Valid Parenthese
  2. // 两遍扫描,时间复杂度 O(n),空间复杂度 O(1)
  3. // @author 曹鹏
  4. class Solution {
  5. public:
  6. int longestValidParentheses(string s)
  7. {
  8. int answer = , depth = , start = -;
  9. for (int i = ; i < s.size(); ++i) {
  10. if (s[i] == '(') {
  11. ++depth;
  12. } else {
  13. --depth;
  14. if (depth < ) {
  15. start = i;
  16. depth = ;
  17. } else if (depth == ) {
  18. answer = max(answer, i - start);
  19. }
  20. }
  21. }
  22. depth = ;
  23. start = s.size();
  24. for (int i = s.size() - ; i >= ; --i) {
  25. if (s[i] == ')') {
  26. ++depth;
  27. } else {
  28. --depth;
  29. if (depth < ) {
  30. start = i;
  31. depth = ;
  32. } else if (depth == ) {
  33. answer = max(answer, start - i);
  34. }
  35. }
  36. }
  37. return answer;
  38. }
  39. };

[LeetCode] Longest Valid Parentheses的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  6. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  7. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  8. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

  9. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. PowerDesigner导出SQL时自动生成注释

    在powerBuilder中新建一个Physical Data Model,在其中新建一个用户表,信息如下图所示: 此时的SQL语句可从其中的Preview视图中得到,如下图所示: 这个时候生成的sq ...

  2. jquery.ui.widget详解

    案例详解 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  3. 【CodeForces 698A】Vacations

    f[i][0..2]表示第i天休息|运动|比赛最少的休息天数. #include <cstdio> #include <cstring> #include <algori ...

  4. 【poj3141】 Distant Galaxy

    http://poj.org/problem?id=3141 (题目链接) 题意 给出平面上n个点,找出一个矩形,使边界上包含尽量多的点. solution 不难发现,除非所有输入点都在同一行或同一列 ...

  5. [NOIP2015] 提高组 洛谷P2678 跳石头

    题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石(不 ...

  6. dedecms /install/index.php.bak Installation File Not Deleted && Executed Via Apache Analytic Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 概括梳理一下这个漏洞的成因 . dedecms默认情况下安装完成之后,i ...

  7. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  8. CF 84D Doctor(二分)

    题目链接: 传送门 Doctor time limit per test:1 second     memory limit per test:256 megabytes Description Th ...

  9. Beta版本冲刺第七天 12.13

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 问题困难 心得体会 胡泽善 用户评价的查看以及审核 用户详情的加入,并且修改了一些卡 ...

  10. HTML5系列四(特征检测、Modernizr.js的相关介绍)

    Modernizr:一个HTML5特征检测库 Modernizr帮助我们检测浏览器是否实现了某个特征,如果实现了那么开发人员就可以充分利用这个特征做一些工作 Modernizr是自动运行的,无须调用诸 ...