LeetCode刷题(20)
来源:互联网 发布:天谕美女捏脸数据 编辑:程序博客网 时间:2024/06/10 16:19
Longest Valid Parentheses
在一个由”(”、”)”组成的字符串中,寻找最长匹配长度
本题的关键在于要记住匹配序列之前的位置,pos用来记录这一数据
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype: int """ pos = [] n = len(s) res = 0 for i in range(n): if s[i] == '(': pos.append(i) else : if len(pos) and s[pos[-1]] == '(': pos.pop() pre = -1 if len(pos): pre = pos[-1] res = i-pre if i-pre> res else res else: pos.append(i) return res
阅读全文
0 0
- Leetcode刷题(20)
- LeetCode刷题(20)
- 【leetcode】leetcode 刷题 笔记 (不定期更新)
- leetcode刷题(Python)
- LeetCode刷题(1)
- LeetCode刷题(2)
- LeetCode刷题(3)
- LeetCode刷题(废弃)
- LeetCode刷题(FizzBuzz)
- Leetcode刷题(1)
- Leetcode刷题(2)
- Leetcode刷题(3)
- Leetcode刷题(4)
- Leetcode刷题(5)
- Leetcode刷题(6)
- Leetcode刷题(7)
- Leetcode刷题(13)
- Leetcode刷题(17)
- 51nod 1378 夹克老爷的愤怒[贪心][树形dp?]
- 用Hexo搭建个人博客
- 第八周项目4-稀疏矩阵的三元组表示的实现及应用(2)
- 文章标题
- 一个不了解HTTP协议的程序员不是一个好的Web开发者
- LeetCode刷题(20)
- 4 Values whose Sum is 0 POJ
- 《Discrete Mathematic with Applications》读书笔记二
- C语言#define之宏定义的使用
- gcc入门
- RabbitMQ之Python
- c语言中数组长度的计算
- 在Eclipse中安装Hibernate Tools
- 1.编写手机类型;2.输出张三李四的姓名...;3.小组内各成员信息显示