GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/032_Longest_Valid_Parentheses.py at master · xguo7013/leetcode

Expand file tree

/

032_Longest_Valid_Parentheses.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
47 lines (45 loc) · 1.45 KB
/

032_Longest_Valid_Parentheses.py

Copy path

File metadata and controls

47 lines (45 loc) · 1.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pdb
class Solution(object):
# def longestValidParentheses(self, s):
# """
# :type s: str
# :rtype: int
# """
# ls = len(s)
# start = [0] * (ls + 1)
# all = [0] * (ls + 1)
# for i in reversed(range(ls - 1)):
# if s[i] == '(':
# if s[i + 1] == ')':
# start[i] = 2
# if start[i + 1] + i + 1 < ls and s[start[i + 1] + i + 1] == ')':
# start[i] = 2 + start[i + 1]
# if start[start[i] + i] > 0:
# start[i] += start[start[i] + i]
# all[i] = max(start[i], all[i + 1])
# return all[0]
def longestValidParentheses(self, s):
# https://leetcode.com/discuss/87988/my-easy-o-n-java-solution-with-explanation
ls = len(s)
stack = []
data = [0] * ls
for i in range(ls):
curr = s[i]
if curr == '(':
stack.append(i)
else:
if len(stack) > 0:
data[i] = 1
data[stack.pop(-1)] = 1
tep, res = 0, 0
for t in data:
if t == 1:
tep += 1
else:
res = max(tep, res)
tep = 0
return max(tep, res)
if __name__ == '__main__':
s = Solution()
# print s.longestValidParentheses(")(((((()())()()))()(()))(")
print s.longestValidParentheses(')()())')
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.