Longest Valid Parentheses 解答
Question
Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
For "(()"
, the longest valid parentheses substring is "()"
, which has length = 2.
Another example is ")()())"
, where the longest valid parentheses substring is "()()"
, which has length = 4.
Solution 1 -- Stack
Parenthese类的题目,第一反应是用栈。遇到'('入栈,遇到')'出栈。这题关键在于怎样知道当前的valid parenthese length。
我们用一个变量start来表示当前的第一个'(' 。
( ( ( ) ) ) ) ) (
start start
当栈为空时,进来的第一个左括号的位置为当前的start值。
出栈操作后:
1. 如果栈为空,说明从start开始的括号都匹配了。因此长度为 current - start + 1
2. 如果栈不为空,我们知道弹出栈的一定是都已经匹配了的。因此长度为 current - stack.peek()
因为只扫描了一遍原数组,所以时间复杂度是O(n),空间复杂度是O(n)
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int length = s.length();
Deque<Integer> stack = new LinkedList<Integer>();
int start = 0;
int result = 0;
for (int i = 0; i < length; i++) {
char current = s.charAt(i);
if (current == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
start = i + 1;
} else {
stack.pop();
result = stack.isEmpty() ? Math.max(result, i - start + 1) : Math.max(result, i - stack.peek());
}
}
}
return result;
}
}
Solution 2 -- DP
Longest Valid Parentheses 解答的更多相关文章
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- redis 学习笔记一
找了半天,发觉还是redis的源码看起来比较舒服.所以决定今年把redis的源码读一遍顺便做个读书笔记.好好记录下.话说现在越来不越不愿意用脑袋来记录东西,喜欢靠note来记.话说这样不爱用脑会不会过 ...
- Android 读取手机某个文件夹目录及子文件夹中所有的txt文件
1. activity_main.xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- JAVA并发实现三(线程的挂起和恢复)
package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...
- jQuery获取select option
jQuery的一些方法理出一些常用的方法: //获取第一个option的值 $('#test option:first').val(); //最后一个option的值 $('#test option: ...
- (转)Android中截取当前屏幕图片
该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面.实现的代码如下: /** * 获取和保存当前屏幕的截图 */ priv ...
- 优化移动体验的HTML5技巧
简介 连轴转的刷新,不断变向的页面转换,以及tap事件的周期性的延迟仅仅是现在移动web环境令人头疼事情的一小部分.开发者正试图尽可能的靠近原生应用,但却经常被各种兼容问题,系统复位,和僵化的框架打乱 ...
- ashx实现文件下载以及文件MD5码测试
cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System ...
- oracle函数之replace
replace('将要更改的字符串','被替换掉的字符串','替换字符串'): ','****') from tmall_tcmessage; 输出为 '158****3367'
- 如何判断Linux load的值是否过高
1.先使用top看下CPU占用高的进程,找出进程的进程ID(pid): 查看方法:top 2.根据进程ID(pid)查看是进程的那些线程占用CPU高. 查看方法:top -Hp pid 3.使用pst ...
- Codeforces Round #350 (Div. 2)A,B,C,D1
A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...