任务一:有效的括号

题目链接:https://leetcode-cn.com/problems/valid-parentheses/

自己的答案:

 class Solution:
def isValid(self, s):
s = list(s)
length = len(s) #空字符串被认为是有效字符串
if length == 0:
return True stringList = ['(', ')', '[', ']', '{', '}']
for s_element in s:
if s_element not in stringList:
return False counter1 = 0
counter2 = 0
counter3 = 0
for s_ele in s:
if s_ele == "(":
counter1 += 1
elif s_ele == ")":
counter1 -= 1
elif s_ele == "[":
counter2 += 1
elif s_ele == "]":
counter2 -= 1
elif s_ele == "{":
counter3 += 1
elif s_ele == "}":
counter3 -= 1
if counter1 == 0 and counter2 == 0 and counter3 == 0:
return True
else:
return False

官方答案:

 class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
""" # The stack to keep track of opening brackets.
stack = [] # Hash map for keeping track of mappings. This keeps the code very clean.
# Also makes adding more types of parenthesis easier
mapping = {")": "(", "}": "{", "]": "["} # For every bracket in the expression.
for char in s: # If the character is an closing bracket
if char in mapping: # Pop the topmost element from the stack, if it is non empty
# Otherwise assign a dummy value of '#' to the top_element variable
top_element = stack.pop() if stack else '#' # The mapping for the opening bracket in our hash and the top
# element of the stack don't match, return False
if mapping[char] != top_element:
return False
else:
# We have an opening bracket, simply push it onto the stack.
stack.append(char) # In the end, if the stack is empty, then we have a valid expression.
# The stack won't be empty for cases like ((()
return not stack
												

leetcode_day01的更多相关文章

随机推荐

  1. Node.js 的初体验

    例子1: 1.首先第一步 :要 下载 node.js. 官网 上可以下载 下载完后,是这个玩意. 2. 打开 node.js ,然后输入 // 引入http模块 var http = require( ...

  2. eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>

    eclipse环境Dynamic web module version 3.1版本之前,Dynamic web object 中Servlet类的配置,要在web.xml 配置<Servlet& ...

  3. Vue 前端md5加密

    用户注册时将加密后的密码发送给后端存储 当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配. npm: https://www.npmjs.com/package/crypto-browseri ...

  4. java.lang.UnsupportedOperationException: Exception occurred during processing request: null

    1.Action有问题,Struts2注解拼写错误,注解包版本不匹配! 2.今天还有一个错误,Tomcat服务器异常,无法启动,Remove/clean后还是无法启动 :极大可能是web.xml 写错 ...

  5. MySQL工作经验

    以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...

  6. 执行pip命令时遇到 Fatal error in launcher: Unable to create process using '"'

    电脑同时安装了python-2.7.13跟python-3.6.1,安装时勾选了pip,环境变量也已经配置好. 为了方便运行,同时修改了可执行文件为 python2和python3.此时在cmd命令行 ...

  7. (转)Unity 和 Cocos2d-x 越渐流行,国内公司开发「自研游戏引擎」的意义何在?

    分几个角度来说:一.我认为,Unity3D将无可挽回的,或者说,势在必得的,成为接下来很多年内,世界移动领域游戏引擎市场霸主.回顾历史,正如同咱们经历过一次又一次的互联网时代变革一样,x86,wind ...

  8. 课时16.HTML-XHTML-HTML5区别(了解)

    简而言之 HTML语法非常宽松容错性强: XHTML更为严格,它要求标签必须小写,必须严格闭合,标签中的属性必须使用引号引起等等. HTML5是HTML的下一个版本所以除了非常宽松容错性强以外,还增加 ...

  9. iOS常用控件-UITableViewCell

    一. 封装cell: 1.加载xib文件的两种方式 <方式1> (NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] ...

  10. git rebase -i

    git rebase -i 作用: 合并提交 示例: 如图所示: 原因: 出现了两个第十一章的提交信息, 其实提交内容是一样的, 但是提交概述不一样. 这就让我很不爽. 我想把两次的概述信息合并为一个 ...