题目简述:

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.

解题思路:

简单的栈的使用,前括号压栈,后括号弹栈。

class Solution:
# @return a boolean
def isValid(self, s):
sta = []
for i in s:
if i in ['(','[','{']:
sta.append(i)
elif i == ')':
if len(sta) < 1 or sta[len(sta)-1] != '(':
return False
else:
sta.pop()
elif i == ']':
if len(sta) < 1 or sta[len(sta)-1] != '[':
return False
else:
sta.pop()
else:
if len(sta) < 1 or sta[len(sta)-1] != '{':
return False
else:
sta.pop()
if len(sta) != 0:
return False
return True

【leetcode】Valid Parentheses的更多相关文章

  1. 【LeetCode】Valid Parentheses(有效的括号)

    这道题是LeetCode里的第20道题. 题目要求: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...

  2. 【LeetCode】Valid Parentheses合法括号

    给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...

  3. 【LeetCode】Generate Parentheses 解题报告

    [题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  4. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【Leetcode】【Easy】Valid Parentheses

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

  6. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  7. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  8. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  9. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

随机推荐

  1. MVC防止xss攻击 ——Html.AntiForgeryToken的AJAX提交

    1.在Html表单里面使用了@Html.AntiForgeryToken()就可以阻止CSRF攻击. 2.相应的我们要在Controller中也要加入[ValidateAntiForgeryToken ...

  2. Android studio 软件板块

  3. ibatis 轻松入门

    1.总中的配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig ...

  4. JavaScript - 正则表达式

    正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...

  5. http://www.microsoft.com/en-pk/download/details.aspx?id=40762

    http://www.microsoft.com/en-pk/download/details.aspx?id=40762

  6. php中计算二维数组中某一元素之和

    [0] => array(5) { ["id"] => string(2) "11" ["name"] => string ...

  7. linQ学习笔记之三高级语句

    linq语句查询执行的时机 第一步获取数据源 int [] obejct = new int[]{1,2,3,4,5,6,7,8,9} 第二步定义查询 var even = numbers.where ...

  8. zabbix触发器依赖

    触发器依赖 Zabbix - Router1 - Router2 – Host 如果router1宕机了,那么router2和host都不能连上,这样的话就会发router1.router2和host ...

  9. python基础八

    面向对象的好处 更容易扩展.提高代码使用效率,使你的代码组织性更强, 更清晰,更适合复杂项目的开发 封装 把功能的实现细节封装起来,只暴露调用接口 继承 多态 接口的继承 定义 类   ===> ...

  10. C#多线程 线程的启动

    在实例化Thread的实例,需要提供一个委托,在实例化这个委托时所用到的参数是线程将来启动时要运行的方法.在.net中提供了两种启动线程的方式,一种是不带参数的启动方式,另一种是带参数的启动的方式. ...