32. Longest Valid Parentheses (JAVA)
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) {
int[] dp = new int[s.length()]; //longest valid parentheses end with current index
Stack<Integer> stack = new Stack<>(); //index of each left parenthese
int ret = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '('){
stack.push(i);
}
else if(!stack.empty()){ //')' and have left parenthese in the stack
if(stack.peek() > 0)
dp[i] = dp[stack.peek()-1] + (i-stack.peek()+1);
else //first index
dp[i] = i-stack.peek()+1;
stack.pop();
}
else{ //')' and have no left parenthese in the stack
stack.clear();
}
}
for(int i = 0; i < s.length(); i++){
if(dp[i]>ret) ret = dp[i];
}
return ret;
}
}
最长有效括号不仅与当前stack的情况有关,也与有效做括号之前的stack情况有关,所以用动态规划。
使用一维动态规划记录以当前位置结束的最长有效括号。
32. Longest Valid Parentheses (JAVA)的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- [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 ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【Python】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Spring Cloud云架构 - commonservice-sso服务搭建(一)
前面几篇我们已经介绍了Spring Cloud和oauth2的知识点,今天我们要利用Spring Cloud和oauth2进行commonservice-sso服务搭建,本节我们只是搭建commons ...
- JavaWEB开发03——JS
今日任务 使用JS完成页面定时弹出广告 使用JS完成表单的校验 使用JS完成表格的隔行换色 使用JS完成复选框的全选效果 使用JS完成省市的联动效果 JS控制下拉列表左右选择 教学导航 掌握JS中的B ...
- Oracle与MySQL的概念区别
MySQL MySQL是一个以用户为中心的概念,一个用户下,拥有多个数据库,一个数据库下拥有多个数据库表!然而Oracle与MySQL有很大的不同!! Oracle Oracle中,一个数据库拥有多个 ...
- 使用SharpZIpLib写的压缩解压操作类
使用SharpZIpLib写的压缩解压操作类,已测试. public class ZipHelper { /// <summary> /// 压缩文件 /// </summary&g ...
- leetcode-mid-others-621. Task Scheduler
mycode 53.01% 这个题在纸上画一画就知道啦,只要出现次数最多的字母能够满足要求,其他更少的字母穿插在其中,间隔就更满足<n啦,当然,最后不要忘记加上尾巴哦,尾巴和出现次数最多的字 ...
- leetcode-mid-math-29. Divide Two Integers-NO
mycode 91.28% class Solution(object): def divide(self, dividend, divisor): """ :typ ...
- Mac下安装lightgbm
Mac下安装lightgbm 1.安装环境 系统 MacOS Mojave 版本10.14.2 Xcode 10.1 $ clang -v Apple LLVM version 10.0.0 (cla ...
- .Net 逆向 Reflector之reflexil使用
网上下载了一款商用的教育培训类软件,是用.Net写的,标榜的是免费的,但是只能试用一个月,商家很精明,用此方法推广招揽客户,但是公司在这一块却没有预算购买,一开始就想着既然是商用软件,安全机制做的肯定 ...
- Delphi回车键切换焦点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- es为什么要取消type? 或者为什么一个index下多个type会有问题
同一个index下的不同的type下的相同的filed,在同一个index下其实会被认为是同一个filed. 否则,不同type中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降