当前位置:网站首页>Longest valid bracket
Longest valid bracket
2022-07-01 00:19:00 【Star_.】
Give you one that only contains ‘(’ and ‘)’ String , Find the longest effective ( The format is correct and continuous ) The length of the bracket substring .
Enhanced bracket matching , So we all want to use stack to do , In fact, it can also be used dp To do it
Method 1 :
Utilization stack :
class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> q = new Stack<Integer>();
int len = s.length();
int ans=0;
q.push(-1);
for(int i=0;i<len;i++){
if(s.charAt(i)=='(')
q.push(i);
else{
q.pop();
if(q.isEmpty())
q.push(i);
else
ans = Math.max(ans,i-q.peek());
}
}
return ans;
}
}
Method 2 :
dp
class Solution {
public int longestValidParentheses(String s) {
int len = s.length();
int dp[] = new int [len+5];
int ans=0;
for(int i=1;i<len;i++){
if(s.charAt(i)==')'){
if(s.charAt(i-1)=='('){
dp[i] = (i>=2?dp[i-2]:0)+2;
}
else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '('){
dp[i]=dp[i-1]+(i-dp[i-1]>=2?dp[i-dp[i-1]-2]:0)+2;
}
ans = Math.max(ans,dp[i]);
}
}
return ans;
}
}
still dp fast
边栏推荐
- New trend of embedded software development: Devops
- 20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
- Advanced mathematical modeling
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
- Error 2059 when Navicat connects to MySQL
- 20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
- 1175. Disposition des nombres premiers / échange de doigts II 104. Nombre de permutations
- What SQL statements are supported for data filtering
- 2022-2028 global encrypted external hard disk industry research and trend analysis report
- What value should testers play in requirements review? Two minutes will stop you from being stupid
猜你喜欢

How to edit special effects in VR panorama? How to display detailed functions?

2022-2028 global PTFE lined valve industry research and trend analysis report

ABAQUS 2022 latest edition - perfect realistic simulation solution

什么是SRM系统,如何规范公司内部采购流程

5g smart building solution 2021

2022-2028 global rampant travel industry research and trend analysis report

Ditto set global paste only text shortcuts

CentOS install MySQL

20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking

Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
随机推荐
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
composer
2022-2028 global plant peptone industry research and trend analysis report
2022-2028 global rotary transmission system industry research and trend analysis report
What SQL statements are supported for data filtering
SSM integration process (integration configuration, function module development, interface test)
Examples of topological sequences
C WinForm program interface optimization example
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
Combining online and offline, VR panorama is a good way to transform furniture online!
Dell r720 server installation network card Broadcom 5720 driver
Bridge emqx cloud data to AWS IOT through the public network
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
Software engineering best practices - project requirements analysis
什么是SRM系统,如何规范公司内部采购流程
在指南针上买基金安全吗?
Matlab saves triangulation results as STL files
Yboj mesh sequence [Lagrange interpolation]
HP notebook disable touchpad after mouse is inserted
Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)