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 ...
随机推荐
- python编程之变量和简单的数据结构
一.变量 前面我们用python输出了“hello world!” 这次我们在前面加入一行,定义一个变量,然后修改第二行. 添加变量导致Python解释器需要做更多工作.处理第1行代码时,它将文本“H ...
- RK3288 uboot启动流程
VS-RK3288嵌入式板卡 U-boot 启动流程小结 bl board_init_f -> crt0.S initcall_run_list(init_sequence_f) - ...
- XSSearch 说明文档保存
XSSearch All Packages | 属性 | 方法(函数) 包 XS 继承关系 class XSSearch » XSServer » XSComponent 版本 1.0.0 源代码 s ...
- java(二)Web部分
2.1.1讲一下http get和post请求的区别? GET和POST请求都是http的请求方式,用户通过不同的http的请求方式完成对资源(url)的不同操作.GET,POST,PUT,DELET ...
- OC重写init方法
在创建一个对象的时候我们经常会用到init方法,单单是init只能是初始化,当我们在初始化的时候想要给这个对象加上默认的东西的时候, 系统提供的init方法就不能满足我们的需要,这时,就需要我们自己去 ...
- linux系统下安装redis以及java调用redis
关系型数据库:MySQL Oracle 非关系型数据库:Redis 去掉主外键等关系数据库的关系性特性 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2. ...
- 怎么取cxgrid某一列的合计值
怎么取cxgrid某一列的合计值 1.cxGrid1DBTableView1->optionsview->Footer 设为True 2.cxGrid1DBTableView1-> ...
- Chapter 8 The Simplest Plug-in Solution
This chapter introduces the simplest plug-in solution that are applicable to the four major componen ...
- 友链 & 日记
友链 & 日记 关于 \(Owen\) 温州中学初三 \(OIer\),目前 \(OI\) 水平一般,文化课成绩浮在中游.喜欢二次元,喜欢听音乐,标准宅一枚.虽然入宅时间很短 欢迎大家跟 \( ...
- Python面向对象1:类与对象
Python的面向对象- 面向对象编程 - 基础 - 公有私有 - 继承 - 组合,Mixin- 魔法函数 - 魔法函数概述 - 构造类魔法函数 - 运算类魔法函数 # 1. 面向对象概述(Objec ...