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. Qt button和buttons区别

    假设我的鼠标左键已经按下.若移动鼠标,会发生的move事件,button返回Qt::NoButton,buttons返回LeftButton.再按下右键,会发生press事件,button返回Righ ...

  2. CAS Client集群环境的Session问题及解决方案 不能退出登录

    casclient源代码下载链接:https://github.com/apereo/java-cas-client cas官网链接:https://www.apereo.org/projects/c ...

  3. JavaScript基本内容

    注释: /*多行 注释*/ //单行注释 变量: //变量均为对象,常用类型:String.Number.Boolean.Array.Object var value = "hello&qu ...

  4. xdg-open命令智能打开各种格式的文件

    在linux中,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less.但当需要打开其他格式文件时,比如pdf.jpg.mp3格式文件,咱们通常做法是进入到文件所在的目录, ...

  5. tornado关于AsyncHTTPClient的使用笔记

    先来一段同步的httpclient使用代码 url = 'https://www.baidu.com/' http_client = HTTPClient() response = http_clie ...

  6. Spring Security 中的加密BCryptPasswordEncoder

    // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...

  7. 力扣(LeetCode)7.整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...

  8. tips 移入悬浮功能

    前景: 页面部分区域需要移入悬浮效果,当然默认的 title 也是可以的,最多只是格格不入,但是却是最为靠谱的.. 思路: 基于 jq 实例扩展 .使用立即执行函数保持功能独立. 自定义类实现功能封装 ...

  9. 使用@JsonFormat引起的时间比正常时间慢8小时解决方法

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")

  10. Java面试题基础知识(收集)

    1.集合类:list和Set比较,各自的子类比较(Arraylist,Vector,inkedLIst,HashSet,TreeSet) List:存入元素有序,元素可以重复,允许null值得存在,主 ...