leetcode-algorithms-32 Longest Valid Parentheses

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

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

解法

使用栈来解决,碰到"("入栈,")"出栈,用字符串的位置减去出栈位置就可获得长度,找出最大值就是需要求的长度.

class Solution {
public:
int longestValidParentheses(string s) {
if (s.size() == 0)
return 0; int max = 0;
std::stack<int> st;
st.push(-1);
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == '(')
st.push(i);
else
{
st.pop();
if (st.empty())
st.push(i);
else
{
int t = i - st.top();
max = max > t ? max : t;
}
}
} return max;
}
};

时间复杂度: O(n).n是字符串长度.

空间复杂度: O(n).n是字符串长度.

链接: leetcode-algorithms 目录

leetcode-algorithms-32 Longest Valid Parentheses的更多相关文章

  1. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  2. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  3. leetcode problem 32 -- Longest Valid Parentheses

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

  4. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

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

  5. 【LeetCode】32. Longest Valid Parentheses

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

  6. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  7. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

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

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

  9. leetcode 32. Longest Valid Parentheses

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

  10. Java [leetcode 32]Longest Valid Parentheses

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

随机推荐

  1. ETCD网络层实现(待完成)

    ETCD系列之三:网络层实现 ETCD系列之二:部署集群 ETCD系列之一:简介 ETCD相关介绍--整体概念及原理方面

  2. 用spring tool suite插件创建spring boot项目时报An internal error occurred during: "Building UI model". com/google/common/

    本文为博主原创,未经允许不得转载 在用spring tool suite创建spring boot项目时,报一下异常: 查阅很多资料之后发现是因为装的spring tool suite的版本与ecli ...

  3. jquery选择器扩展之样式选择器

    https://github.com/wendux/style-selector-jQuery-plugin http://blog.csdn.net/duwen90/article/details/ ...

  4. js为什么返回两个对象字符串 objcet objcet ?

    js中两个使用 toString() 对有个有对象的数组进行操作时,为什么返回两个对象字符串 objcet objcet ? [{}].toString(); 返回 "[object Obj ...

  5. sublime配置 sublimecondeintel 分号后不要提示

    https://github.com/SublimeCodeIntel/SublimeCodeIntel/issues/461 Thanks to @catgsmith ,I find a simil ...

  6. 常用模块(subprocess/hashlib/configparser/logging/re)

    一.subprocess(用来执行系统命令) import os cmd = r'dir D:xxx | findstr "py"' # res = subprocess.Pope ...

  7. sed命令使用详解

        内容来自马哥视频,感谢马哥精彩讲解 sed:编辑器 sed: Stream EDitor, 行编辑器,逐行进行处理 grep:实现文本过滤 awk:文本报告生成器 sed默认不编辑源文件,而是 ...

  8. 1 --- Vue 基础指令

    1.vue 指令 1.v-model  主要在表单中使用,文本框.teaxare.单选.下拉 等 2.v-text   文本渲染  类似{{}} 3.v-show  控制Dom显示隐藏   displ ...

  9. Python 基础 Python是什么

    1.Python 是一门高级的.面向对象的,解释性,脚本语言.

  10. python字符串用法

    一,数据类型的转换 1常用的数据类型有int,str,bool三种; int是整型:一般的操作是查找数据的二进制长度bit_length() 一般数据有: 1)二进制:逢二进一 2)十进制,(0-9) ...