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 ...
随机推荐
- Nuget CsvHelper 的使用
CsvHelper:nuget地址 csv导出类||生成类 public class CSVHeader { public string head1 { get; set; } public stri ...
- 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-|1-1-0.dll
今天想把自己电脑上的python2换成python3时,安装完python3后,命令行启动时需要出现了上述错误,在网上查了资料后应该是库文件遭到了破坏,于是我下了一个东西安装后就解决了,如果出现了此问 ...
- File操作-将txt里的内容写入到数据库表
package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...
- 记时,耗时,Stopwatch
public static string InvokeStopwatch(Action function) { System.Diagnostics.Stopwatch sw = new System ...
- [原][源码][tinyxml][opencv]按照规格剪切所有的图片
源码: #include <iostream> #include <fstream> #include <opencv2/core/core.hpp> #inclu ...
- Spring Bean后置处理器
本例子源于:W3CSchool,在此作记录 Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己 ...
- Codeforces 960F - Pathwalks
960F - Pathwalks 思路: ORZ 杜老师 用map写1e5个树状数组,骚操作 记Q为query和update次数,则节点个数约为Q*log(N) 代码: #include<bit ...
- [C#]读取不同版本的excel文件的方法
--------------------------------2007及以上的版本-------------------------------- 测试如下: //DataInterface.Met ...
- selenium+Page Objects(第一话)
简单介绍一种selenium用来做web自动化测试的设计模式:Page Objects 一.Page Objects介绍 用官话说它是selenium中的一种页面对象设计模式(不是测试框架!是一种开展 ...
- 数据结构(C语言版)-第5章 树和二叉树
5.1 树和二叉树的定义 树(Tree)是n(n≥0)个结点的有限集,它或为空树(n = 0):或为非空树,对于非空树T:(1)有且仅有一个称之为根的结点:(2)除根结点以外的其余结点可分为m(m& ...