指数:[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. easyui LinkButton

    http://www.zi-han.net/case/easyui/menu&button.html

  2. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

  3. ufldl学习笔记和编程作业:Softmax Regression(softmax回报)

    ufldl学习笔记与编程作业:Softmax Regression(softmax回归) ufldl出了新教程.感觉比之前的好,从基础讲起.系统清晰,又有编程实践. 在deep learning高质量 ...

  4. 修改字符串中特定的内容,用于OpenRecovery Script

    下面的是实例内容 目标是把OpenRecovery Script输入的内容进行修改 当有下面的输入:(作用是安装/emmc目录下面的update-signed.zip 刷机包) install /em ...

  5. POJ - 3249 Test for Job (DAG+topsort)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  6. iOS 8 新特性

    这篇文章会介绍iOS8开发相关的主要特性. App 插件 通过支持插件,iOS8让我们可以系统指定的区域进行扩展,也就是为用户的特定需求提供自定义的方法.例如:可以通过App插件帮助用户分享他们的内容 ...

  7. 实验数据结构——KMP算法Test.ming

    翻译计划     小明初学者C++,它确定了四个算术.关系运算符.逻辑运算.颂值操作.输入输出.使用简单的选择和循环结构.但他的英语不是很好,记住太多的保留字,他利用汉语拼音的保留字,小屋C++,发明 ...

  8. String和StringBuffer 常用方法总结

     String和StringBuffer 常用方法总结 一.不可变长度String 1.字符串---->char数组 char[] chars=str.toCharArray(); 2.字符串中 ...

  9. 取缔Chrome装载电脑管家的广告过滤脚本代码

    今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...

  10. ios 多线程开发(一)简介

    简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...