LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
package leetcode_50; /***
*
* @author pengfei_zheng
* 最长合法的括号问题
*/
public class Solution32 {
public static int longestValidParentheses(String s) {
int right = 0 , left = 0, ans = 0;
int len = s.length();
for(int i = 0 ; i < len ; i++){
if(s.charAt(i)=='(')
left++;
else
right++;
if(left == right){
ans = Math.max(ans,2*right);
}
else if(right>left)
left = right = 0;
}
left = right = 0;
for(int i = len-1 ; i >= 0 ; i--){
if(s.charAt(i)==')')
right++;
else
left++;
if(right == left)
ans = Math.max(ans,2*left);
else if(left>right)
left = right = 0;
}
return ans;
}
public static void main(String[]args){
String s="()()";
System.out.println(longestValidParentheses(s));
}
}
LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)的更多相关文章
- [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] 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 ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- 032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
- [LeetCode] 32. Longest Valid Parentheses (hard)
原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
随机推荐
- Linux入门教程:如何检查Linux系统的最后重启时间
问题: 是否有一个命令可以快速地检查系统已经运行了多久? 也就是我怎么知道Linux系统最后的重启时间? 有许多方法来查询系统最后的重启时间. 方法一 第一种方法是使用last命令. $ last r ...
- js 事件调度
var EventTarget = function () { this._listener = {}; }; EventTarget.prototype = { constructor: this, ...
- 配置防盗链 访问控制Directory 访问控制FilesMatch
- Fedora26 tftp-server设置
安装tftp-server yum install -y tftp-server 启动软件 systemctl start tftp.socket systemctl enable tftp.soc ...
- PHP安全之临时文件的安全
(一)临时文件简介临时文件,顾名思义是临时的文件,文件的生命周期短.然而,很多应用的运行都离不开临时文件,临时文件在我们电脑上无处不在,通常有以下几种形式的临时文件: 文件或图形编辑程序,所生成的中间 ...
- Tensorflow同时加载使用多个模型
在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...
- AndroidManifest详解
一,重要性AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根目 ...
- lua 对表的简单序列化与反序列化
参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718 function sz_T2S(_t) local szRet = " ...
- 使用dshow捕获摄像头图像
#include "stdafx.h" #include <DShow.h> #include <Guiddef.h> #include <strmi ...
- linux 查看硬件信息
1.查看内存槽数.那个槽位插了内存,大小是多少 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range ...