leetcode-algorithms-32 Longest Valid Parentheses
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-32 Longest Valid Parentheses的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
随机推荐
- 题解——code[vs] 1506 传话(传递闭包)
裸的传递闭包 直接Floyd暴力即可 #include <cstdio> #include <algorithm> #include <cstring> using ...
- HDU 5119 Happy Matt Friends(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...
- 根据元素取两个list<T>不同
var aa = ltB.FindAll(b => ltA.Any(a => a.PolicyNo == b.ID)); //得出不同 var expectedList = ltB.Exc ...
- 【BZOJ】4013: [HNOI2015]实验比较
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4013 中第i 条涉及的图片对为(KXi, Xi),判断要么是KXi < Xi ,要么 ...
- hdu 4349 Xiao Ming's Hope 规律
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- @Value("#{}")与@Value("${}")的区别以及用法
package com.ieou.capsule_basic_info.util; import org.springframework.beans.factory.annotation.Value; ...
- 《EM-PLANT仿真技术教程》读书笔记
1.在系统分析过程中,必须考虑系统所处的环境,因此划分系统与环境的边界是系统分析的首要任务 2.模型可以分为物理模型和数学模型.数学模型可以分为解析模型.逻辑模型.网络模型以及仿真模型.模型可以分为离 ...
- 配置多个版本的jdk
引言: 由于公司有多个项目都需要我开发,而各个项目所依赖的jdk版本又不同,因此需要配置多个jdk; 配置方法: 什么也不说了,直接上图: 然后在其他需要制定Tomcat的地方直接引用JAVA_HOM ...
- dll多个版本问题
在配置文件设置不同版本的dll即可 配置文件如下 configuration 节点下面的 runtime 节点新增各个版本配置内容 <runtime> <assemblyBindi ...
- django序列化 serializers
Django的序列化工具让你可以将Django的模型‘翻译’成其它格式的数据.通常情况下,这种其它格式的数据是基于文本的,并且用于数据交换\传输过程. 一.序列化数据 Django为我们提供了一个强大 ...