当前位置:网站首页>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
边栏推荐
- Error 2059 when Navicat connects to MySQL
- 女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
- Bridge emqx cloud data to AWS IOT through the public network
- Pycharm is very fast to learn from installation to full armament. There are so many pictures because it is too detailed!
- 20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
- Summer Challenge [FFH] harmonyos mobile phone remote control Dayu development board camera
- [designmode] singleton pattern
- 1175. Disposition des nombres premiers / échange de doigts II 104. Nombre de permutations
- 1175. 质数排列 / 剑指 Offer II 104. 排列的数目
- 1175. prime number arrangement / Sword finger offer II 104 Number of permutations
猜你喜欢

Ditto set global paste only text shortcuts

2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report

The programmer's girlfriend gave me a fatigue driving test

2022-2028 global ICT test probe industry research and trend analysis report

ABAQUS 2022 latest edition - perfect realistic simulation solution

Bridge emqx cloud data to AWS IOT through the public network

2022-2028 global ethylene oxide scrubber industry research and trend analysis report

Maxpool2d explanation -- Application in arrays and images

Redis - understand the master-slave replication mechanism

20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
随机推荐
需求评审,测试人员应该发挥怎样的价值?两分钟让你不再懵逼
Analysis of 8253a register
Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions
Combining online and offline, VR panorama is a good way to transform furniture online!
Mysql database query optimization
Simple application example of rhai script engine
Arthas debugging problem determination Toolkit
How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?
Using Excel to quickly generate SQL statements
Redis - sentinel mode
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
Five minutes to understand the exploratory test
Software supply chain security risk pointing North for enterprise digitalization and it executives
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
IFLYTEK active competition summary! (12)
2022-2028 global plant peptone industry research and trend analysis report
ABAQUS 2022 software installation package and installation tutorial
shell 同时执行多任务下载视频
让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
C /platform:anycpu32bitpererrored can only be used with /t:exe, /t:winexe and /t:appcontainerexe