【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
【032-Longest Valid Parentheses(最长有效括号)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
给定一个字符串,仅仅包括小括号号。求最长的合法的小括号的数目。
解题思路
使用栈来实现
代码实现
算法实现类
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;
public class Solution {
public int longestValidParentheses(String s) {
// 用于记录待匹配的左括号和右括号的位置
Stack<Integer> st = new Stack<>();
int max = 0;
for (int i = 0; i < s.length(); i++) {
// 如是当前字符是右括号,而且记录栈非空。而且前一个字符是左括号
if (s.charAt(i) == ')' && !st.isEmpty() && s.charAt(st.peek()) == '(') {
// 左括号出栈
st.pop();
// 求最大值
max = Math.max(max, i - ((st.isEmpty()) ? -1 : st.peek()));
}
// 其他情况就将字符入栈
else {
st.push(i);
}
}
return max;
}
}
评測结果
点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47064939】
【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】的更多相关文章
- 032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses最长有效括号
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31
32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...
随机推荐
- watch监听
watch: { getTitle:{ handler:function(val,oldval){ }, deep:true//对象内部的属性监听,也叫深度监听 }, },
- 设置div内的内容不能被选中
通过简单的css设置页面的文字无法被选定. <div class="select">我不能被选中复制</div> .select{ -webkit-user ...
- ES6学习笔记(二十二)ArrayBuffer
ArrayBuffer ArrayBuffer对象.TypedArray视图和DataView视图是 JavaScript 操作二进制数据的一个接口.它们都是以数组的语法处理二进制数据,所以统称为二进 ...
- canvas 连线曲线图
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...
- javaweb集成swagger
一.添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
- keepalive安装配置
安装 Centos7.4 yum install keepalived 配置 Master服务器配置 [root@wsjy-proxy01 keepalived]# cat keepalived.co ...
- C语言实现的minixml解析库入门教程
minixml的中文说明手册:MiniXML中文文档.dochttp://wenku.baidu.com/view/25fd7d7f31b765ce050814f7.html xml源代码: < ...
- Python学习第一天-编写登陆接口
编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 帐号文件user.txt内容如下: qaz 123qwe 12345qweqwr 12321424...... 锁文件user_l ...
- Regular Expressions Syntax
https://support.syslogwatcher.com/support/solutions/articles/8000033627-regular-expressions-syntax
- drupal7 怎样将一个date字段加入上日期插件效果
//这里以created字段为样例 function Hook_form_alter($form,$form_state,$form_id){ $form['created']['#type'] = ...