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 — githubTest/20_ValidParentheses.py at master · yychuyu/githubTest

Expand file tree

/

20_ValidParentheses.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
27 lines (26 loc) · 1010 Bytes
/

20_ValidParentheses.py

Copy path

File metadata and controls

27 lines (26 loc) · 1010 Bytes
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
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) == 0:
return True
elif len(s) % 2 == 1 or s[0] == ")" or s[0] == "]" or s[0] == "}":
return False
else:
d = {"(": ")", "[": "]", "{": "}"}
tmp = s[0]
for parenthese in s[1:]:
tmp = tmp + parenthese
# if parenthese != d[tmp[-1]] or parenthese != "(" or parenthese != "[" or parenthese != "{":
# return False
#len(tmp)>=2是为了防止诸如()[]的情况,{}消除后第一个tmp长度只有1,程序中断
if len(tmp) >= 2 and parenthese == d[tmp[-2]]:
tmp = tmp[:-2]
elif parenthese != "(" and parenthese != "[" and parenthese != "{":
return False
if len(tmp) == 0:
return True
else:
return False
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.