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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  5. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  8. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  9. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

随机推荐

  1. tab.js分享及浏览器兼容性问题汇总

    在 样式布局分享-基于frozen.js的移动OA 文章中,用了到第三方组件 tab.js(带菜单的横屏滑动插件),其兼容性很差,进行优化后,已兼容全平台(且支持IE6+). tab.js GitHu ...

  2. 《think in python》学习-9

    think in python think in python -9 案例分析:文字游戏 从文本文件中读取文字 作者提供了一个文本文件words.txt 地址 本章后面案例也会用带该文件中的词组 fi ...

  3. jQuery--checkbox全选/取消全选

    用JavaScript使页面上的一组checkbox全选/取消全选,逻辑很简单,实现代码也没有太难的语法.但使用jQuery实现则更简单,代码也很简洁,精辟! jQuery版本:1.3.2 <h ...

  4. css系列教程--border和background

    css标签b:1.background:用法 background:可以指定颜色,背景,平铺效果以及背景定位.background:url(aa.png) #fff 0px 0px no-repeat ...

  5. synchronized 关键字

    synchronized 多用于并发不高并且需要单线程运行的地方.比如你有一个A方法,你在方法上加了synchronized修饰.那么两个人同时去调用这个方法的时候不是并行的,是抢占的,谁先抢到资源谁 ...

  6. JSP(二)

    JSTL JSTL不仅可以实现EL所不能实现的逻辑循环或者条件判断,还有强大的定制标记. 使用JSTL 需要将两个文件("jstl.jar"和"standard.jar& ...

  7. js 保留两位小数

    1. 最笨的办法....... function get() { var s = 22.127456 + ""; var str = s.substring(0,s.indexOf ...

  8. JavaScript学习笔记(二)原型

    JavaScript不包含传统的类继承模型,而是使用prototype原型模型.JavaScript使用原型链的继承方式. function Foo() { this.value = 42; } Fo ...

  9. Linux02--文件系统与磁盘管理

    1.文件默认权限umask    umask命令用于指定新建文件和目录时的默认权限.    root的umask默认值是022,普通用户的umask值为002.    新建文件的默认权限=666 - ...

  10. 各种非标232,485协议,自定义协议转modbus协议模块定制开发,各种流量计协议转modbus,

    工业现场经常会碰到通过485或者232采集各类仪表数据,但是很多早期的仪表和设备不支持标准modbus协议,而是采用自定义的协议,这些协议数据由plc或者dcs系统来实现采集,不仅费时麻烦,而且不方便 ...