国产探花免费观看_亚洲丰满少妇自慰呻吟_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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旅游| 贵德县| 庆云县| 南皮县| 许昌县| 塘沽区| 定边县| 松滋市| 阿坝| 二连浩特市| 武宣县| 商南县| 凌海市| 锦屏县| 鹿邑县| 元谋县| 开封县| 大同市| 绥滨县| 普格县| 屏边| 抚远县| 龙江县| 长海县| 额敏县| 秀山| 广德县| 临潭县| 行唐县| 青铜峡市| 阿拉善盟| 长乐市| 贡嘎县| 肇源县| 华容县| 连南| 密云县| 鄂伦春自治旗| 合川市| 宜宾县| 西安市|