62.Longest Valid Parentheses(最长的有效括号)
Level:
Medium
题目描述:
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 "()()"
思路分析:
设置一个栈,遍历字符串,如果遇到‘(’,将它对应的下标存放进栈,遇到‘)’,其下标为i,弹出栈顶元素,计算 i-stack.pop()更新res,直到遍历结束,得到最大的res。
代码:
public class Solution{
public int longestValidParentheses(String s){
Stack<Integer>stack=new Stack<>();
int left=-1;
int res=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='(')
stack.push(i);
else{
if(stack.isEmpty())
left=i; //一开始出现‘)’
else{
stack.pop();
if(stack.isEmpty())
res=Math.max(res,i-left);
else
res=Math.max(res,i-stack.peek());
}
}
}
return res;
}
}
62.Longest Valid Parentheses(最长的有效括号)的更多相关文章
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 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 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
- 32. Longest Valid Parentheses最长有效括号
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 高级定时器-setTimeout()、setInterval()、链式setTimeout()
使用 setTimeout()和 setInterval()创建的定时器可以用于实现有趣且有用的功能.执行时机是不能保证的,因为在页面的生命周期中,不同时间可能有其他代码在控制 JavaScript ...
- XMPP即时通讯协议使用(六)——开发Openfire聊天记录插件
转载地址:http://www.cnblogs.com/hoojo/archive/2013/03/29/openfire_plugin_chatlogs_plugin_.html 开发环境: Sys ...
- rpc - rpc 程序号数据库
SYNOPSIS /etc/rpc DESCRIPTION rpc 文件列出了rpc 程序的可读名, 可以此代替rpc 程序号. 每行包含如下信息: 运行rpc 程序的服务名 rpc 程序号 别名 各 ...
- 330-支持PXIE带FMC接口的Xilinx FPGA XC7K325T PCIeX8 接口卡平台
支持PXIE带FMC接口的Xilinx FPGA XC7K325T PCIeX8 接口卡平台 一.板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_ ...
- django 常用 import
from django.shortcuts import HttpResponse, render, redirect def yimi(request): #直接返回页面内容 return Http ...
- Sass-@each
@each 循环就是去遍历一个列表,然后从列表中取出对应的值. @each 循环指令的形式: @each $var in <list> 如果你没有接触过列表,也不要紧,他也非常简单. 在下 ...
- 分布式理论 BASE、CAP、ACID
CAP原理: 在理论计算机科学中,CAP定理(CAP theorem),又被称作布鲁尔定理(Brewer's theorem),它指出对于一个分布式计算系统来说,不可能同时满足以下三点: 一致性(Co ...
- C++ KMP文本匹配
代码如下: 环境为VC #include <iostream> #include <algorithm> #include <string> #include &l ...
- java Map的四种遍历方式
1.这是最常见的并且在大多数情况下也是最可取的遍历方式,在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Int ...
- SpringBoot---监控与管理actuator
1.概述 SpringBoot在Start POMS中提供了一个特殊依赖模块spring-boot-starter-actuator: 引入spring-boot-starter-actuator模块 ...