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. 题解——P1133 教主的花园DP

    直接设二维状态具有后效性,会爆零 然后需要加一维 然后70 看了题解之后发现没有考虑1和n的关系 考虑之后,四十 然后懵逼 突然发现自己的ans更新写错了,导致每次ans都是第一个取30的情况的解 然 ...

  2. SQLServer2008 远程过程调用失败

    今天在连接数据库的时候,发现无法获取到服务器名称,打开SQLServer Configuration Manager,发现SQLServer服务中远程过程调用失败 我装的是VS2017,在网上百度了一 ...

  3. CAS 是什么

    CAS是英文单词Compare And Swap的缩写,翻译过来就是比较并替换. CAS机制当中使用了3个基本操作数:内存地址V,旧的预期值A,要修改的新值B. 更新一个变量的时候,只有当变量的预期值 ...

  4. javaSE习题 第一章 JAVA语言概述

    转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...

  5. leecode第六十二题(不同路径)

    class Solution { public: int uniquePaths(int m, int n) { ||n==) ; vector<vector<int>> pa ...

  6. win10新系统修改onedrive目录,提示找不到OneDrive目录

    win10不知更新了什么,x1c非常卡一跳一跳的,很多年没见过了-_-!!( 原因排查:http://www.cnblogs.com/xuanmanstein/p/8878180.html). 于是重 ...

  7. 从flask视角理解angular(四)Route

    app-routing.modules.ts import { NgModule } from '@angular/core'; import { RouterModule, Routes } fro ...

  8. Java中的包扫描(工具)

    在现在好多应用场景中,我们需要得到某个包名下面所有的类, 包括我们自己在src里写的java类和一些第三方提供的jar包里的类,那么怎么来实现呢? 今天带大家来完成这件事. 先分享代码: 1.这个类是 ...

  9. TypeError: atlas.getSpriteFrame is not a function

    1.资源结构如下: 2.在使用cc.loader.loadRes动态异步加载cc.SpriteAtlas资源时出现这个错误,代码如下: var self = this; var url = " ...

  10. 原生dom事件注册和移除事件的封装

    var addEvent = (function() { var setListener; setListener = false; return function(el, ev, fn) { if ...