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 ...
随机推荐
- Google C++ style guide——头文件
1.#define保护 使用#define防止头文件被多重包括.命名格式为:<PROJECT>_<PATH>_<FILE>_H_ 比如,foo中的头文件foo/sr ...
- 什么是 CSS 预处理器?
什么是 CSS 预处理器? 就CSS本身而言,对于大多数Web前端从业人员来说就不是问题.学过CSS的人都知道,它不是一种编程语言.你可以用它开发网页样式,但是没法用它编程.换句话说,CSS基本上是 ...
- Android开发环境的搭建之(四)虚拟设备AVD的基本配置
配置Android系统语言包为中文简体.点击导航栏左数第二个“菜单”图标 选择“System settings”. 选择“Language & Input” 选择“Language” 选择“中 ...
- maven将jar包安装到本地仓库的命令
进入cmd 执行以下命令: mvn install:install-file -Dfile=E:\sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -Dar ...
- AfxEnableControlContainer()
1)OLE(Object Linking and Embedding,对象连接与嵌入).是一种面向对象的技术,利用OLE可开发可重复使用的软件组件(COM). 2)ActiveX 控件是基于组件对象模 ...
- VS2008 自动化编译脚本
可以通过调用MSBuild来使VS2008进行自动化编译. 1.新建文本文件,后缀名改为bat 2.文件内加上: ;转到MSBuild.exe路径 c: cd\ cd C:\Windows\Micro ...
- Python基础:绑定和方法调用
首先,方法仅仅是类内部定义的函数,也就是说,方法是类属性而不是实例属性. 其次方法有两种被调用的方式:调用绑定的方法和调用未绑定的方法. 当存在一个实例时,方法才被认为绑定到了那个实例上,没有实例时方 ...
- linux 系统分区方案建议
前言: 以前初识Linux时,对Linux系统安装时分区的选择,一点都不了解,导致几次没法进行下一步安装,因此就静下心来,专门拿出时间研究了研究这方面的知识: 以下内容就是以前通过研究Linux安装过 ...
- DataGridview 填写数字
private DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit private void dgv ...
- Java 集合嵌套List of List
在LeetCode上遇到这样返回值 public class Solution { public List<List<Integer>> levelOrder(TreeNode ...