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 ...
随机推荐
- c# 将object尝试转为指定对象
主方法: /// <summary> /// 将object尝试转为指定对象 /// </summary> /// <param name="data" ...
- excle记录
比较两列不一样的数据 https://jingyan.baidu.com/article/fd8044fa23eef05030137a66.html
- php hash_file
string hash_file ( string $algo , string $filename [, bool $raw_output = FALSE ] ) 参数¶ algo 要使用的哈希算法 ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- JS模块化工具require.js教程(一):初识require.js
随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求 ...
- lucene之Field属性的解释
Field类 数据类型 Tokenized是否分词 Indexed 是否索引 Stored 是否存储 说明 StringField(FieldName, FieldValue,Store.YES)) ...
- Lombok轮子
前提 自从进公司实习后,项目代码中能用 Lombok 的都用了,毕竟这么好的轮子要充分利用好.也可以减少一些 get/set/toString 方法的编写,虽说 IDEA 的插件可以自动生成 get/ ...
- memset函数用法
1. memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组 c:是赋给buffer的值 cou ...
- 实现一个simple 3层的神经网络
1.基本概念 1.1softmax softmax函数:一句话概括:是logistic 函数的扩展,将一个p维的数值向量映射成为一个k维的概率值,且这k个值的和为1. 公式: 解释: 1.2 cros ...
- spring boot 入门及示例
需要环境:eclipse4.7.3 + jdk1.8 +maven3.6.1 + tomcat(web需要) spring boot官网介绍:https://spring.io/guides/gs/s ...