导航页-LeetCode专题-Python实现

相关代码已经上传到github:https://github.com/exploitht/leetcode-python

文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo

1、读题

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.

这是经典的栈相关的题,凡是学习数据结构,一般书籍提到栈都会提及括号匹配,解析括号组成的字符串,如果是左括号就入栈,如果是右括号就出栈,看是否匹配。结束的时候再判断一发栈是否为空就行。

2、解题

这道题比较简单,上面读题已经讲到用栈来实现,代码逻辑很简单,如下:

class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# 循环读取字符串s,如果是左括号则进栈,如果是有括号则出栈
# 出栈的时候栈不能为空,循环结束的时候栈需要为空
pars = {'(': 1, '[': 2, '{': 3, ')': -1, ']': -2, '}': -3}
stack = list()
for par in s:
if pars.get(par) > 0:
stack.append(par)
else:
if len(stack) > 0:
if pars.get(stack.pop()) == abs(pars.get(par)):
continue
else:
return False
else:
return False
return True if len(stack) == 0 else False

LeetCode专题-Python实现之第20题:Valid Parentheses的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第1题:Two Sum

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. 2018-2019-2 网络对抗技术 20162329 Exp4 恶意代码分析

    目录 Exp4 恶意代码分析 一.基础问题 问题1: 问题2: 二.系统监控 1. 系统命令监控 2. 使用Windows系统工具集sysmon监控系统状态 三.恶意软件分析 1. virustota ...

  2. git commit时暂时忽略已提交的文件

    当正在修改某文件A,此时需要commit,但是A没修改完暂时不能一起commit. 执行: git update-index --assume-unchanged A的路径 git暂时会忽略该文件的修 ...

  3. freebsd 时间校准

    修改 /etc/rc.conf ntpdate_enable='YES'ntpd_enable='YES' 如果这里不指定ntpdate_hosts=参数的话,ntpdate会读取/etc/ntp.c ...

  4. Windows10用fiddler抓包Android应用(解决手机设置代理后无法上网,设置只抓app包)

    1.环境准备 1.电脑上安装fiddler 2.手机和电脑在同一个局域网内 2.设置 1.fiddler>Tools>Fiddler Options>Connections 勾选Al ...

  5. Android 性能优化之使用Lint

    代码静态检查推荐: (1)Android自定义Lint实践 (2)美团外卖Android Lint代码检查实践 (3)Android自定义Lint实践2——改进原生Detector Lint 是 An ...

  6. 在VB中动态执行VBS代码,可操控窗体控件

    通过执行一段VBS代码来操控窗体内的控件也可以使用AddObject方法添加自己的类,那么在动态VBS代码中也一样可以使用在增加程序扩展性或是有脚本化需求的时候,这个方法还是不错的. Option E ...

  7. github错误:fatal: remote origin already exists.

    原文链接:http://blog.csdn.net/dengjianqiang2011/article/details/9260435 如果输入$ Git remote add origin git@ ...

  8. NOIP-比例简化

    题目描述 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有 1498 人,反对的有 902 人,那么赞同与反对的比例可以简单的记为 1498:902 . ...

  9. sortable的基本属性

    所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象   ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象    ...

  10. vue图片上传到七牛云

    代码: <template> <div class="upload-info"> <div> <el-upload class=" ...