leetcode — longest-valid-parentheses
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的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [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
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Notepad++编写运行python程序
Notepad++编写运行python程序. 1.菜单栏->语言->P->Python设置语言为Python 2.写好代码后ctrl+s保存文件为py文件 3.菜单栏->运行, ...
- Win7 VS2017编译Blender2.79
去年在VS2013环境编译过一次,重装系统后换了VS2017,正好刚编译完Godot3.0.2,顺手把Blender也编译了吧. 官方Windows下编译指南 https://wiki.blender ...
- JAVA执行远端服务器的脚本
JAVA执行远端服务器的脚本 问题描述 实现思路 技术要点 代码实现 问题描述 工作中遇到这样一个问题,我们的应用为了实现高可用会采取双机部署,拓扑图大致如下: 这种方案可以简单的保证高可用,即便应用 ...
- 微信二次开发点击菜单openId的获取
首先普及一个知识:一个关注的用户对于一个微信公众号是唯一的,也就是说一个用户针对与一个微信公众号是唯一的,对于不同的公众号,同一个微信号具有不同的openId; 在微信开发中,我们添加了一个二级菜单并 ...
- Windows批处理命令学习中遇到的坑--持续更新中
再次拾起windows批处理命令,下边将一些遇到的小问题写出来,希望可以帮到大家 1.set命令:set主要的作用是为变量赋值,类似于编程语言中的var i = Value:但是在使用的过程中一定要注 ...
- Javaweb 编解码流程
参考: https://www.ibm.com/developerworks/cn/java/j-lo-chinesecoding/#N10263 https://www.cnblogs.com/ch ...
- ubuntu设置ssh登录
1.为ubuntu添加root用户(其实Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号),打开初始账号,输入命令:sudo passwd root,设置root的密码 2.切 ...
- 我的C#跨平台之旅(五):使用IoC之依赖注入实现
引入NuGet包:Unity 实现接口:IDependencyResolver 在启动类中注入依赖的类: 注意:左框中的内容为接口或抽象类,右框中为实际要注入的类. 修改控制类,使用构造方法注入类: ...
- jQuery 基础语法
jQuery介绍 1.jQuery是一个轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Aja ...
- Hive(一)
1. HIVE概念: Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本 ...