当前位置:网站首页>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
边栏推荐
- Is it safe to choose mobile phone for stock trading account opening in Hangzhou?
- Combining online and offline, VR panorama is a good way to transform furniture online!
- 1. crawler's beautifulsoup parsing library & online parsing image verification code
- Redis - cache penetration, cache breakdown, cache avalanche
- 让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
- How to use robots Txt and its detailed explanation
- IFLYTEK active competition summary! (12)
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
- Development of wireless U-shaped ultrasonic electric toothbrush
- 2022-2028 global rampant travel industry research and trend analysis report
猜你喜欢
Redis - cache penetration, cache breakdown, cache avalanche
2022-2028 global rampant travel industry research and trend analysis report
[leetcode] [SQL] notes
2022-2028 global elevator emergency communication system industry research and trend analysis report
Why did kubernetes win? The changes in the container circle!
1. crawler's beautifulsoup parsing library & online parsing image verification code
Development of wireless U-shaped ultrasonic electric toothbrush
6-1 exploit -ftp exploit
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
When is it appropriate to replace a virtual machine with a virtual machine?
随机推荐
Is it safe to open a stock account of Huatai Securities online?
Pycharm is very fast to learn from installation to full armament. There are so many pictures because it is too detailed!
Redis - cache penetration, cache breakdown, cache avalanche
What does it mean to open an account online? Is it safe to open an account online?
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
lvm-snapshot:基于LVM快照的备份之准备工作
"Experience" my understanding of user growth "new users"
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main()
Maxpool2d explanation -- Application in arrays and images
2022-2028 global plant peptone industry research and trend analysis report
2022-2028 global elevator emergency communication system industry research and trend analysis report
How do it outsourcing resident personnel position their pain points?
Matlab saves triangulation results as STL files
SSM integration process (integration configuration, function module development, interface test)
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
网上开华泰证券的股票账户是否安全呢?
Dell r720 server installation network card Broadcom 5720 driver
Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"