Longest Valid Parentheses
来源:互联网 发布:配音软件手机软件 编辑:程序博客网 时间:2024/05/16 08:17
题目:
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.
简单
借助栈
参考代码:
感觉大神做这种简单题,就是大材小用啊,不过,再大的神也是从这种小题做出来的啊~
http://codeganker.blogspot.com/2014/03/longest-valid-parentheses-leetcode.html
public class Solution { public int longestValidParentheses(String s) { if(s==null || s.length()==0) return 0; LinkedList<Integer> stack = new LinkedList<Integer>(); int start = 0; int max = 0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='(') { stack.push(i); } else { if(stack.isEmpty()) { start = i+1; } else { stack.pop(); max = stack.isEmpty()?Math.max(max,i-start+1):Math.max(max,i-stack.peek()); } } } return max;}}
0 0
- LeetCode: Longest Valid Parentheses
- LeetCode Longest Valid Parentheses
- LeetCode: Longest Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- [leetcode]longest Valid Parentheses(!!)
- Longest Valid Parentheses
- Longest Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- leetcode Longest Valid Parentheses
- LeetCode31:Longest Valid Parentheses
- longest valid parentheses
- Longest Valid Parentheses
- 【leetcode】Longest Valid Parentheses
- Longest Valid Parentheses
- [LeetCode]Longest Valid Parentheses
- Longest Valid Parentheses
- Longest Valid Parentheses
- Longest Valid Parentheses
- linux I2C驱动分析(二)
- javascript接口实现
- HDU 2680 Choose the best route(Dijkstra)
- mysql忘记root密码
- JSP知识总结
- Longest Valid Parentheses
- Spring MVC请求过程
- c++ primer 练习5.20
- 1037. Magic Coupon (25)
- Linux基本命令(二)
- DirectX11 聚光灯
- 马克飞象-图文教程
- Aizu 2450 Do use segment tree LCT
- AI---个人主页