Write a called function that accepts a bracket string and determines whether the order of the brackets is valid

Title description:

  

Write a function called that takes a string of parentheses, and determines if the order of the parentheses is valid. 
The function should return true if the string is valid, and false if it's invalid.

我的解答:
  
def valid_parentheses(string):
conn = 0
for i in string:
if i == '(':
conn += 1
if i == ')':
conn -= 1
if conn < 0:
return False
if conn == 0:
return True
else:
return False

Guess you like

Origin www.cnblogs.com/wlj-axia/p/12692338.html
Recommended