国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Leetcode] 20. Valid Parentheses

2019-11-08 02:01:14
字體:
來源:轉載
供稿:網友

PRoblem:

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Idea: Use the stack to solve this problem. For every incoming left mark, just push into the stack. Then, for every incoming right mark, just check the top item in the stack, if top item matches the incoming right mark, pop the top item, else return False. Finally, after going throuth all items in string, just check if the stack is empty or not, if it is empty(means all pairs are matched and popped) then return True, else return False.

Solution:

class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ l = list() for item in s: if item == '(' or item == '{' or item == '[' : l.append(item) elif item == ')': if len(l) == 0: return False elif l[len(l)-1] == '(': l.pop() else: return False elif item == '}': if len(l) == 0: return False elif l[len(l)-1] == '{': l.pop() else: return False elif item == ']': if len(l) == 0: return False elif l[len(l)-1] == '[': l.pop() else: return False if len(l) == 0: return True else: return False
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丘北县| 华蓥市| 怀来县| 仙桃市| 阳西县| 开化县| 巴马| 翼城县| 信阳市| 类乌齐县| 金坛市| 界首市| 历史| 柯坪县| 莫力| 华池县| 灵山县| 漳浦县| 定州市| 华池县| 绥棱县| 南和县| 礼泉县| 张家港市| 崇左市| 民勤县| 安溪县| 兰坪| 公主岭市| 阳朔县| 江门市| 万荣县| 镇江市| 邵东县| 安宁市| 惠安县| 武鸣县| 丰都县| 宜都市| 新疆| 博乐市|