当前位置:网站首页>2022-024arts: Longest valid bracket
2022-024arts: Longest valid bracket
2022-07-26 08:15:00 【FlashSu】
ARTS:Algorithm、Review、Tip、Share
- Algorithm Algorithm problem
- Review English articles
- Tip Think back to a small skill learned at work this week
- Share Think about a technical point of view 、 Social hot spots 、 A product or a puzzle
Algorithm
32. Longest valid bracket
Give you one that only contains ‘(’ and ‘)’ String , Find the longest effective ( The format is correct and continuous ) The length of the bracket substring .
Example 1:
Input :s = “(()”
Output :2
explain : The longest valid bracket substring is “()”
Example 2:
Input :s = “)()())”
Output :4
explain : The longest valid bracket substring is “()()”
Example 3:
Input :s = “”
Output :0
Tips :
0 <= s.length <= 3 * 104
s[i] by ‘(’ or ‘)’
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-valid-parentheses
Solution description
A typical topic of dynamic programming , The point is to find out 、 Define the state transition equation , Create a dp Array , Its elements represent : Must contain the subscript element corresponding to the current input character array The longest valid bracket value of . We just need to find out dp The value of each element of the array , Finding the maximum value is the goal of the topic .
dp The solution of the value is related to the corresponding character : If it is an open bracket , Currently, it is definitely not a valid bracket combination , Just skip ; So we only deal with closed parentheses in the current position “)” The situation of . If the previous element is an open bracket , It's simpler , Direct view dp[i-2] Add the value of the element 2 Just fine , Pay attention to the need to judge the subscript . If i-1 The position is closed parenthesis “)”, You need to keep looking forward , Until we find the possible correspondence i Open bracket of position “(”, here dp[i] = dp[i-1] + dp[i-dp[i-1] - 2] + 2, among i-dp[i-1] - 2 by dp[i-1] The effective length goes forward past the previous position of the open bracket , That is x Location .
coded
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int max = 0;
int[] dp = new int[s.length()];
char[] chars = s.toCharArray();
for (int i = 1; i < chars.length; i++) {
if (chars[i] != ')') {
continue;
}
if (chars[i - 1] == '(') {
// see ( Whether the previous longest string is valid
dp[i] = i >= 2 && dp[i - 2] >= 0 ? dp[i - 2] + 2 : 2;
} else if (dp[i - 1] > 0 && (i - dp[i - 1] - 1) >= 0 && chars[i - dp[i - 1] - 1] == '(') {
// ↓
// x((……))
dp[i] = dp[i-1] + (i-dp[i-1]-2 >= 0 ? dp[i-dp[i-1]-2] : 0) + 2;
}
max = Math.max(max, dp[i]);
}
return max;
}
Complexity analysis
Time complexity O(n)
Spatial complexity O(n)
Review
Spring 5 source code analysis series (4) — initialization of IOC container (2)
Tip
check 2 Whether the timestamps are the same day , It is necessary to formulate a specific time zone for judgment , And check whether it is the same day , According to Java8 Medium LocalDate whether
public static void main(String[] args) {
ZoneId swissZone = ZoneId.of("Asia/Shanghai");
ZoneId numZone = ZoneId.of("UTC+08:00");
System.out.println(swissZone.equals(numZone));
Instant instant = Instant.now();
ZonedDateTime time1 = instant.atZone(swissZone);
ZonedDateTime time2 = Instant.ofEpochMilli(System.currentTimeMillis()).atZone(numZone);
System.out.println("is same date? " + time1.toLocalDate().isEqual(time2.toLocalDate()));
System.out.println("is same date, use equals? " + time1.toLocalDate().equals(time2.toLocalDate()));
System.out.println("time is equals? " + time1.isEqual(time2));
}
Share
I've seen it recently 《 The courage to be hated 》 This book , It mainly introduces some viewpoints in Adler's Psychology , The book is described in the form of several dialogues between young people and philosophers , Explained freedom 、 Happiness 、 The meaning of life 、 Attitude to life these problems , I believe this is a topic that many people will think about , My own words , There will always be some vague understanding , The ideas given in this book are simple 、 Much more direct , But it is worth repeatedly understanding and thinking ……
边栏推荐
- 要不你给我说说什么是长轮询吧?
- JSP action -- usebean action
- Zroi easy sum (generating function, block, DP, combination, polynomial)
- 99 multiplication table and inverted triangle 99 multiplication table
- 【EndNote】文献类型与文献类型缩写汇编
- CentOS install mysql5.7
- Traversal mode of list, set, map, queue, deque, stack
- Team members participate in 2022 China multimedia conference
- 2022/7/1
- 2w字详解数据湖:概念、特征、架构与案例
猜你喜欢

Burp Suite-第一章 Burp Suite 安装和环境配置

有点牛逼,一个月13万+

一键部署LAMP和LNMP架构

2W word detailed data Lake: concept, characteristics, architecture and cases

Burp Suite-第九章 如何使用Burp Repeater

C# 获取选择文件信息

An empirical study on urban unemployment in Guangxi (Macroeconomics)

Table fix specific rows

Recurrence of strtus2 historical vulnerability

苹果强硬新规:用第三方支付也要抽成,开发者亏大了!
随机推荐
正则表达式作业
The most complete network: detailed explanation of six constraints of MySQL
Template summary
一键部署LAMP和LNMP架构
How WPS sets page headers page by page
The difference between FileInputStream and bufferedinputstream
JSP action -- usebean action
Function default parameters, arrow functions, and remaining parameters in ES6 - explanation
【EndNote】文献类型与文献类型缩写汇编
The difference between ArrayList and LinkedList
Matlab drawing black spots on two / three-dimensional drawings
Burp Suite-第六章 如何使用Burp Spider
Burp suite Chapter 5 how to use burp target
Pycharm code specification tool flake8
Burp Suite-第七章 如何使用Burp Scanner
The difference between LinkedList and ArrayList
Sed job
If the thread crashes, why doesn't it cause the JVM to crash? What about the main thread?
Differences and connections of previewkeydown, Keydown, keypress and Keyup in C WinForm
2022-07-09 group 5 Gu Xiangquan's learning notes day02