首页 > 其他分享 >32. Longest Valid Parentheses

32. Longest Valid Parentheses

时间:2022-11-05 23:36:36浏览次数:29  
标签:index int 32 substring Valid Longest max Input stack

Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.

 

Example 1:

Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".

Example 2:

Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".

Example 3:

Input: s = ""
Output: 0

 

public int longestValidParentheses(String s) {
int max = 0, start = 0;
if(null == s) return 0;

int len = s.length();

Stack<Integer> stack = new Stack<>();
for(int index = 0; index < len; index++){
//遇左括号(,压栈(栈中元素为当前位置所处的下标)
if('(' == s.charAt(index)){
stack.push(index);
continue;
} else {
if(stack.isEmpty()){
start = index+1;
continue;
} else {
stack.pop();
if(stack.isEmpty()){
max = Math.max(max, index-start+1);
} else {
max = Math.max(max, index-stack.peek());
}
}
}
}

return max;
}

标签:index,int,32,substring,Valid,Longest,max,Input,stack
From: https://www.cnblogs.com/MarkLeeBYR/p/16861672.html

相关文章

  • 【FPGA】[VRFC 10-3236] concurrent assignment to a non-net ‘data_out’ is not pe
    写作时间:2021-06-01报错如下:[VRFC10-3236]concurrentassignmenttoanon-net‘data_out’isnotpermitted[“F:/fpgaWork/project_test_gamma/project_test_gamma.......
  • Esp32 Mqtt
    使用esp32连接MQTT服务器使用Arduino库PubSubClient.h修改PubSubClient.h文件基本初始化引入头文件#include"PubSubClient.h"constchar*ssid="XXXX";......
  • 2022-2023-1 20221322《计算机基础与程序设计》第十周学习总结
    作业信息这个作业属于哪个课程<班级的链接>(2022-2023-1-计算机基础与程序设计)这个作业要求在哪里<作业要求的链接>(2022-2023-1计算机基础与程序设计第十周作业......
  • 到2032年,AI平台市场将达到2541.4亿美元
    根据FutureMarketInsights的数据,全球人工智能平台市场预计将在2022年价值100亿美元,到2022-2032年预测期结束时,将以38.2%的CAGR增长,价值2541.4亿美元。2021年该市场价值96......
  • Codeforces Round #832 (Div. 2) C. Swap Game (博弈论)
    https://codeforces.com/contest/1747/problem/CC.SwapGame题目大意:给定一个长度为n的数组a,每次只要当我想动但是发现a[1]==0的时候我就输了要么就是我每次把a[1]......
  • BUUCTF-PWN-第四页writep(32题)
    重感冒持续发热五天,拖到现在终于发第四页的题解了axb_2019_heap保护全开的菜单堆题但是存在格式化字符串漏洞add如果key=43,那么大小可以自定义,不然最小只能是......
  • Codeforces Round #832 (Div2)
    A.TwoGruops将正负数分离为两个集合,得到\(sum_{+},sum_{-}\)。考虑将一个数移到正负性相反的集合中,一定会导致\(sum_{+},sum_{-}\)同时在数轴上向原点移动,差值绝对......
  • Android 明年将不再支持 32 位应用
    上周,Google正式发布了Android13并率先向Pixel设备推送了系统更新,其他OEM厂商也将跟进这一最新系统,并会在今年晚些时候向他们旗下的设备推送更新。近日,MishaalRahma......
  • Codeforces Round #832 (Div. 2) E
    牛逼题。通过拐点刻画路径,这样每条路径的贡献方式唯一,你只要钦定拐点都选即可刻画唯一的路径,然后路径上的其他点随便选。https://codeforc.es/contest/1747/problem/Eht......
  • 2022-2023-1 20201324《信息安全系统设计与实现(上)》第12章
    1块设备I/O缓冲区文件系统使用一系列I/O缓冲区作为块设备的缓存内存。当进程试图读取(dev,blk)标识的磁盘块时,它首先在缓冲区缓存中搜索分配给磁盘块的缓冲区。如果该缓......