导航页-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. java中的反射整理

    1,什么是反射 反射机制是java语言提供的一种基础功能,它能够赋予成语在运行时进行自省的能力.通过反射我们可以直接操作类或者对象,例如:可以通过反射去获取某个对象的类的定义,属性,方法,还可以修改类 ...

  2. C++的编译预处理

    C++中,在编译器对源程序进行编译之前,首先要由预处理对程序文本进行预处理.预处理器提供了一组预编译处理指令和预处理操作符.预处理指令实际上不是C++语言的一部分,它只是用来扩充C++程序设计的环境. ...

  3. python学习:字典

    字典 1.查询内存地址 a = 10 print(id(a)) b = a print(id(b)) b = 15 print(id(b)) 2. 数据类型 不可变类型:整型.字符串.元组 可变类型: ...

  4. 动态规划-LIS1

    https://vjudge.net/contest/297216?tdsourcetag=s_pctim_aiomsg#problem/J #include<bits/stdc++.h> ...

  5. 2018-2019-20175324实验一《Java开发环境的熟悉》实验报告

    2018-2019-20175324实验一<Java开发环境的熟悉>实验报告   实验内容与结果 一.Java开发环境的熟悉-1 1.实验要求: 0 参考实验要求 1 建立“自己学号exp ...

  6. R语言grid包just参数如何just图形位置

    思路   grid的画图函数都含有just,但是just参数的是怎么调节图形位置的总是让人非常费解,于是便写了代码来一探究竟.   思路非常简单:放一个2*2的布局viewport,每个布局里面放一个 ...

  7. Spring Cloud 组件 —— feign

    feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...

  8. laravel5单元测试

    https://www.cnblogs.com/love-snow/articles/7641198.html

  9. Bypass_Disable_functions_Shell

    Bypass_Disable_functions_Shell https://github.com/l3m0n/Bypass_Disable_functions_Shell 一个各种方式突破Disab ...

  10. Winform 窗体获得焦点

    给窗体添加Shown事件 public void Form_Shown(object sender, EventArgs e) { this.Activate(); this.Focus(); //定 ...