题目:

Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:
1.Any left parenthesis '(' must have a corresponding right parenthesis ')'.
2.Any right parenthesis ')' must have a corresponding left parenthesis '('.
3.Left parenthesis '(' must go before the corresponding right parenthesis ')'.
4.'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
5.An empty string is also valid. Example 1:
Input: "()"
Output: True

Example 2:
Input: "(*)"
Output: True

Example 3:
Input: "(*))"
Output: True

Note:
1.The string size will be in the range [1, 100].

解题思路:

题目要求检查括号是否匹配,匹配的规则可以认为和小学数学四则运算里面括号的用法一样,即一个右括号必须对应一个左括号,有对应关系的右括号必须在左括号右边,等等等等。。。题目为了增加一点难度,引入了一个万能符号*,一个*号可以用作一个左括号,也可以用作一个右括号,或者一个空字符。

万变不离其宗,我的解法是遍历输入的string,如果字符是左括号或者*号则按照类似入栈的方式存入数组stack中,如果遇到右括号,则寻找数组stack中最近一次存入的左括号。如果存在,把左括号从数组中删除,继续遍历string;如果不存在左括号,删除最近存入的*号;如果左括号和*号均不存在于数组stack中,则返回这是一个不合法的字符串。说了这么多如果分支,其实原则就是,左括号优先和右括号进行匹配,如果没有左括号,用离右括号最近的*号匹配。

最后,遍历完成string后,还需要检查数组stack中剩余的元素,这时数组stack中只会存在左括号和*号(右括号不会存入stack),还需要再分析一次剩余元素是否合法。检查的算法和上述几乎一样,遍历stack,如果元素是*号,存入数组stack2;如果是左括号,判断stack2是否存在*号;如果没有,说明是非法字符串;如果存在则删掉最近存入stack2的*号,继续循环,直到所有的左括号都有*号与之匹配。

啰啰嗦嗦说了一串,还是上代码吧,写的比较乱。

class Solution(object):
def findLastChar(self,stack):
for i in range(len(stack)):
if stack[i] == '(':
del stack[i]
return True
return False def checkValidString(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for i in s:
if i != ')':
stack.insert(0,i)
else:
isFind = False
while len(stack) > 0:
if stack[0] == '(' :
isFind = True
del stack[0]
break
elif stack[0] == '*':
if self.findLastChar(stack) == False:
del stack[0]
isFind = True
break
else:
del stack[0]
if isFind == False:
return False
stack2 = []
for i in stack:
if i == '*':
stack2.insert(0,i)
else:
if len(stack2) == 0:
return False
del stack2[0]
return True

【leetcode】Valid Parenthesis String的更多相关文章

  1. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  2. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. leetcode 678. Valid Parenthesis String

    678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...

  4. [leetcode]678. Valid Parenthesis String验证有效括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. [LeetCode] 678. Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  6. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  7. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  8. 【LeetCode】394. Decode String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  9. 【leetcode】Valid Palindrome

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

随机推荐

  1. 不错的点击li标签删除的例子

    <script type="text/javascript">function delElement(obj){ obj.parentNode.removeChild( ...

  2. Idea 竖选文本、竖向选择、横向纵向选择文本代码

    在使用Idea的时候,可能需要在相同类型的文字中增加数据,所以Idea提供一种列式选择方式,提高开发的效率. 如果需要使用,我们可以选中代码,右键单击,在弹出的菜单中选中[Column Selecti ...

  3. spring boot-1.简单介绍及环境搭建

    1.简介 spring boot 是在spring 基础上进行了全面整合的架构,个人认为优点在于以下几点: 1.简化配置,甚至零配置即可开发出一个web应用.spring boot 默认配置了大量的s ...

  4. Webstorm 2019激活码(有效期至2020年6月5日)

    Webstorm 2019激活码(有效期至2020年6月5日):https://blog.csdn.net/lt326030434/article/details/90229298

  5. laravel5.5学习2-路由系统

    一.初识路由 路由系统是所有 PHP 框架的核心,路由承载的是 URL 到代码片段的映射,不同的框架所附带的路由系统是这个框架本质最真实的写照,一丝不挂,一览无余.Laravel 路由中文文档:htt ...

  6. semantic-ui的表单使用

    semantic-ui 的表单使用 最近找了一款ui库,jquery可以使用的.可以进行个性化定制,感觉还不错. 现状 简单阐述下该ui的现状吧,目前止步于2.4的版本,github商讨了一波.大致是 ...

  7. Hibernate 初体验

    为什么会产生 Hibernate Mybatis 这类的dao层框架 传统的jdbc 虽然执行速度很快,但是开发效率很低,随着面向对象开发的设计思想,在面向对象编程中 将对象 进行持久化,存入关系型的 ...

  8. Elasticsearch入门教程(二):Elasticsearch核心概念

    原文:Elasticsearch入门教程(二):Elasticsearch核心概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:ht ...

  9. 高效开发之使用Cmder替换cmd

    一.为什么要更换为cmder 在做项目时,有些时候我想复制控制台上面的代码时,cmd有的时候复制粘贴很麻烦,Cmder则不会,并且Cmder可以分屏多开窗口,可以设置窗口颜色,字体大小,并且很多快捷键 ...

  10. webpack4导入jQuery的新方案

    本文的目的 拒绝全局导入jQuery!! 拒绝script导入jQuery!! 找到一种只在当前js组件中引入jQuery,并且使用webpack切割打包的方案! 测试环境 以下测试在webpack3 ...