[LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n)
- // 使用栈,时间复杂度 O(n),空间复杂度 O(n)
- class Solution {
- public:
- int longestValidParentheses(string s) {
- int max_len = , last = -;
- stack<int> lefts;
- for (int i = ; i < s.size(); ++i) {
- if (s[i] =='(') {
- lefts.push(i);
- } else {
- if (lefts.empty()) {
- // update last
- last = i;
- } else {
- // find a matching pair
- lefts.pop();
- if (lefts.empty()) {
- max_len = max(max_len, i-last);
- } else {
- max_len = max(max_len, i-lefts.top()); }
- }
- }
- }
- return max_len;
- }
- };
第二种方方法,搞一个计数器,每次遇到'('加一,遇到‘)’ 减少一,所以只有在计数器==0的时候更新 max_len。为了实现功能,必须两侧扫描,从而在计数器==0的时候更新max_len.
例如( ( () () 由于左括号多,因此从左侧扫描无法达到计数器==0,所以,max_len还是0.但当从右侧扫描是,右括号较少,会达到0,从而得到真实的max_len.保存start的思想和栈方法一致。
- // LeetCode, Longest Valid Parenthese
- // 两遍扫描,时间复杂度 O(n),空间复杂度 O(1)
- // @author 曹鹏
- class Solution {
- public:
- int longestValidParentheses(string s)
- {
- int answer = , depth = , start = -;
- for (int i = ; i < s.size(); ++i) {
- if (s[i] == '(') {
- ++depth;
- } else {
- --depth;
- if (depth < ) {
- start = i;
- depth = ;
- } else if (depth == ) {
- answer = max(answer, i - start);
- }
- }
- }
- depth = ;
- start = s.size();
- for (int i = s.size() - ; i >= ; --i) {
- if (s[i] == ')') {
- ++depth;
- } else {
- --depth;
- if (depth < ) {
- start = i;
- depth = ;
- } else if (depth == ) {
- answer = max(answer, start - i);
- }
- }
- }
- return answer;
- }
- };
[LeetCode] 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 ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 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 -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- PowerDesigner导出SQL时自动生成注释
在powerBuilder中新建一个Physical Data Model,在其中新建一个用户表,信息如下图所示: 此时的SQL语句可从其中的Preview视图中得到,如下图所示: 这个时候生成的sq ...
- jquery.ui.widget详解
案例详解 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- 【CodeForces 698A】Vacations
f[i][0..2]表示第i天休息|运动|比赛最少的休息天数. #include <cstdio> #include <cstring> #include <algori ...
- 【poj3141】 Distant Galaxy
http://poj.org/problem?id=3141 (题目链接) 题意 给出平面上n个点,找出一个矩形,使边界上包含尽量多的点. solution 不难发现,除非所有输入点都在同一行或同一列 ...
- [NOIP2015] 提高组 洛谷P2678 跳石头
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石(不 ...
- dedecms /install/index.php.bak Installation File Not Deleted && Executed Via Apache Analytic Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 概括梳理一下这个漏洞的成因 . dedecms默认情况下安装完成之后,i ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...
- CF 84D Doctor(二分)
题目链接: 传送门 Doctor time limit per test:1 second memory limit per test:256 megabytes Description Th ...
- Beta版本冲刺第七天 12.13
一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 昨天完成任务 今天完成任务 问题困难 心得体会 胡泽善 用户评价的查看以及审核 用户详情的加入,并且修改了一些卡 ...
- HTML5系列四(特征检测、Modernizr.js的相关介绍)
Modernizr:一个HTML5特征检测库 Modernizr帮助我们检测浏览器是否实现了某个特征,如果实现了那么开发人员就可以充分利用这个特征做一些工作 Modernizr是自动运行的,无须调用诸 ...