leetcode Longest Valid Parentheses python
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
maxlen=0
stack=[]
last=-1
for i in range(len(s)):
if s[i] == '(':
stack.append(i)
else:
if stack == []:
last=i
else:
stack.pop()
if stack == []:
maxlen=max(maxlen,i-last)
else:
maxlen=max(maxlen,i-stack[len(stack)-1])
return maxlen
leetcode Longest Valid Parentheses python的更多相关文章
- [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
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [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][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
随机推荐
- Bostonkey Simple calc
Simple Calc 明显的memcpy栈溢出,是一个静态链接的程序所以没给libc.发现里面有: 参数a1应该为_libc_stack_end的地址了._stack_prot通过rop修改为0x7 ...
- 【递归】【3月周赛1】【Problem B】
Problem B Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- sticker.js贴纸效果
http://stickerjs.cmiscm.com/ <div class="sticker gbtags"></div> <!-- 引用Java ...
- solr4.5分组查询、统计功能介绍
说到分组统计估计大家都不会陌生,就是数据库的group by语句,但是当我们采用solr4.5全文检索时,数据库提供再好的sql语句都没有任何的意义了,那么在solr4.5中我们如何做到分组统计呢?其 ...
- Asp.Net实现Http长连接推送
话说最新帮一个朋友搞智能家居方面的东西,做一个云平台.主要作用手机在局域网外环境时对手机客户端和智能网关中命令的互相转发. 目前已经有了一个稳定的Socket版本,但是考虑到以后的扩展和性能指标要改成 ...
- git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚
[git 删除本地分支] git branch -D br [git 删除远程分支] git push origin :br (origin 后面有空格) git代码库回滚: 指的是将代码库某分支退 ...
- iOS设计模式解析(五)责任链模式
责任链模式:使多个对象都有机会处理请求,从而避免发送者和接受者之间发生耦合. 应用场景: 有多个对象可以处理请求,而处理程序只有在运行时才能确定 例如: 英雄联盟中伤害计算,伤害类型分为AP.AD.真 ...
- KZ--NSString、NSMutableString
//NSString初始化的几种方法(3种方法) //1. NSString *str2 = [[NSString alloc] init]; ...
- [Linked List]Remove Duplicates from Sorted List
Total Accepted: 90247 Total Submissions: 254602 Difficulty: Easy Given a sorted linked list, delete ...
- 如何编写Dll(用命令行编译加深理解)
DLL的优点 简单的说,dll有以下几个优点: 1) 节省内存.同一个软件模块,若是以源代码的形式重用,则会被编译到不同的可执行程序中,同时运行这些exe时这些模块的二进制码会被重复加载到内 ...