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 ...
随机推荐
- qtp识别验证码
花了两天时间才完整的完成识别验证码的登录操作,在网上看到很多关于验证码识别的方法,但是我用的qtp版本比较高级,所以还是要自己花心思研究.po上我的识别验证码的详细历程: 一.读取浏览器中的图片验证码 ...
- [LeetCode]-algorithms-Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 如何隐藏scroll-Y纵向滚动条,并不影响内容滚动的方法
网上搜了很多关于隐藏滚动条的文章,发现很多都是只说了如何隐藏scroll-X横向滚动条,对scroll-Y纵向滚动条并没有明确的述说.本文章将介绍3种隐藏滚动条的方法,大家可以结合实际情况,参考文章内 ...
- 《Effective Java》读书笔记 - 8.通用编程
Chapter 8 General Programming Item 45: Minimize the scope of local variables local variables应该在他们要被用 ...
- gitblit 数据迁移(复制)
gitblit 数据迁移 完全拷贝方式: 将原服务器上的gitblit的安装目录.数据目录等相关目录拷到另一台服务器上即可,这样启动方式和使用端口及数据和原服务上的一模一样.(因为gitblit是不用 ...
- express中redirect传递数据
redirect中无法跟render一样传递数据 在index中,可以通过session重定向到login 在login.js 中获取req.session,渲染到login.ejs中,最后js获取
- 转:C语言inline详细讲解
本文介绍了GCC和C99标准中inline使用上的不同之处.inline属性在使用的时候,要注意以下两点:inline关键字在GCC参考文档中仅有对其使用在函数定义(Definition)上的描述,而 ...
- ES6数值的拓展
二进制和八进制表示法 ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 如果要将0b和0o前缀的字符串数值转为十进制,要使用Number方法 Number(' ...
- Java使用JDBC连接Hive
最近一段时间,处理过一个问题,那就是hive jdbc的连接问题,其实也不是大问题,就是url写的不对,导致无法连接.问题在于HiveServer2增加了别的安全验证,导致正常的情况下,传递的参数无法 ...
- Pager
jQuery var Pager = function (ops) { this._ops = { count: ops.count || 0, selectedIndex: ops.selected ...