import java.util.Stack;

/**
* Source : https://oj.leetcode.com/problems/longest-valid-parentheses/
*
* Created by lverpeng on 2017/7/13.
*
* Given a string containing just the characters '(' and ')',
* find the length of the longest valid (well-formed) parentheses substring.
*
* For "(()", the longest valid parentheses substring is "()", which has length = 2.
*
* Another example is ")()())", where the longest valid parentheses substring is "()()",
* which has length = 4.
*
*/
public class LongestParentheses { /**
* 最长有效的括号字符串
*
* 和之前判断括号匹配的一样使用栈,因为要计算长度,所以栈里面放括号字符的序号
*
* 边界情况:
* 栈为空遇到了右括号,开始压入一个-1,遇到右括号换的时候出栈,并压入新的右括号的序号
*
* @param parentheseStr
* @return
*/
public int longestParentheses (String parentheseStr) {
Stack<Integer> stack = new Stack<Integer>();
int maxLen = 0;
stack.push(-1);
for (int i = 0; i < parentheseStr.length(); i++) {
if (parentheseStr.charAt(i) == '(') {
stack.push(i);
} else {
if (stack.size() > 1) {
stack.pop();
int temp = stack.peek();
if (maxLen < (i - temp)) {
maxLen = i - temp;
}
} else {
stack.pop();
stack.push(i);
}
}
}
return maxLen;
} public static void main(String[] args) {
LongestParentheses longestParentheses = new LongestParentheses();
System.out.println(longestParentheses.longestParentheses("(()"));
System.out.println(longestParentheses.longestParentheses(")()())"));
}
}

leetcode — longest-valid-parentheses的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  5. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  8. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  9. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

  10. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. JS格式化日期时间的方法

    //格式化时间的方法 function format(fmt, date) { var o = { "M+": date.getMonth() + 1, //月份 "d+ ...

  2. Nexus安装、使用说明、问题总结

    Nexus安装.使用说明.问题总结 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在 ...

  3. C#语法快速热身

    if选择结构: 简单:if结构 单分子:If-else结构 多重:f-else-if结构 嵌套:if结构 语法: If(条件1){ If(条件2) }else{ } }else{ } Switch结构 ...

  4. Python数据结构之单链表

    Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...

  5. [转]MYSQL性能查看(命中率,慢查询)

    网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...

  6. weka环境配置

    java环境变量设置: 安装jdk到具体目录"ABC"下当前目录下应该有jdk+版本号和jre加版本号. 然后打开环境变量:新建JAVA_HOME内容是:jdk的安装目录.例如:D ...

  7. SQL给数据编号

    方法:ROW_NUMBER() over(order by getdate()) AS num 使用案例 : select * From (select  ROW_NUMBER() over(orde ...

  8. python 单例模式的四种实现方法及注意事项

    一.模块单例 Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码. #foo1.py clas ...

  9. Shader_ShaderForge_NGUI_流光&波纹&消融

    Shader篇 总结:总算解决了NGUI中Shader不能实时更改的问题,原来NGUI中的Texture组件提供了OnRender代码示例如下 /*************************** ...

  10. Java中不定项参数(可变参数)的作用和使用方式

    引言: 我们在编写方法的过程中,可能会遇见一个方法有不确定参数个数的情况.一般我们会用方法重载来解决问题: //方法重载,解决参数个数不确定问题 public void method(); publi ...