指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


032. Longest Valid Parentheses (Hard)

链接

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

代码(github):https://github.com/illuz/leetcode

题意

问一个字符串里最长的合法括号串的长度。

分析

  1. (C++)用栈来做,假设匹配就出栈,然后长度就是 cur - stack_top_pos 也就是 - 匹配的前一个位置。 O(n) time, O(n) space。
  2. (C++)栈消耗空间太多了。事实上能够维护 () 匹配的长度。只是可能出现 ()))
    ((()
    的情况。所以要前后各扫一遍。

    O(n) time, O(1) space。

  3. 用较复杂的 DP 来做,只是空间可没解法 2 那么优了。刚看到我非常久前的一个解法,用太多空间了Orz。如今来看还是 1、2 的做法好。

代码

解法 1:(C++)

class Solution {
public:
int longestValidParentheses(string s) {
stack<int> lefts;
int max_len = 0, match_pos = -1; // position of first
// matching '(' - 1 for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(')
lefts.push(i);
else {
if (lefts.empty()) // no matching left
match_pos = i;
else { // match a left
lefts.pop();
if (lefts.empty())
max_len = max(max_len, i - match_pos);
else
max_len = max(max_len, i - lefts.top());
}
}
} return max_len;
}
};

解法 2:(C++)

class Solution {
public:
int longestValidParentheses(string s) {
int max_len = 0, depth = 0, start = -1; // solve ((()
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(')
++depth;
else {
--depth;
if (depth == 0)
max_len = max(max_len, i - start);
else if (depth < 0) {
start = i;
depth = 0;
}
}
} // solve ()))
depth = 0;
start = s.size();
for (int i = s.size(); i >= 0; --i) {
if (s[i] == ')')
++depth;
else {
--depth;
if (depth == 0)
max_len = max(max_len, start - i);
else if (depth < 0) {
start = i;
depth = 0;
}
}
} return max_len;
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)的更多相关文章

  1. Java for LeetCode 032 Longest Valid Parentheses

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

  2. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  3. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

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

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. 【leetcode】Longest Valid Parentheses

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

  7. 【leetcode】 Longest Valid Parentheses (hard)★

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

  8. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  9. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

随机推荐

  1. Git代理服务器设置和访问Github

    因为现在工作的网络环境有着非常严格的限制,.可以说,在最近的访问通过代理Github它采取了一些曲折的.也积累了一些相关经验.我们认为有必要注意什么. 符合"不要再发明轮子"宗旨, ...

  2. [文学阅读] METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments

    METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments Satanje ...

  3. IIS在W7下使用

    1.0.发布程序

  4. 【白痴弟弟和你加强应用层】阅读 Develop API Guides 思考(一个)

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 动态检測设备属性 关于targetSdkVersion的含义 关于onSaveInstanceState的高 ...

  5. SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法

    原文:SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法 在new SqlBulkCopy对象的时候,设置一下SqlBulkCopyOptions选项即可,按位或运算 SqlBulkC ...

  6. 人人API 分享到人人功能 修改版

    最近在搞一个日程管理网站, 需要实现分享到人人功能, 所以找了一下人人API, 然后根据自己需要修改了一下. 首先得有一个人人给的js文件, 如下: var Renren = Renren || {} ...

  7. 两个文件中的配置项设置方法和C比较程序处理

    在实际的软件开发项目.程序经常需要翻阅了一些资料可能会改变从外部,我们需要读出的信息到一个统一的文件(一般ini档),而此文件被称为个人资料. 考虑这样一个场景,程序须要与多个数据库打交道,要从配置文 ...

  8. HDU 1274 展开字符串 (递归+string类)

    题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...

  9. C#的WebBrowser控制浏览

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 算法 - 乞讨n中位数(C++)

    //************************************************************************************************** ...