【leetcode】Valid Parenthesis String
题目:
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的更多相关文章
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- leetcode 678. Valid Parenthesis String
678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...
- [leetcode]678. Valid Parenthesis String验证有效括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] 678. Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- 【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
随机推荐
- pycryptodom的源码安装
1.去网站https://pypi.python.org/pypi/pycryptodome/#downloads下载 2.python setup.py build -> python set ...
- node递归批量重命名指定文件夹下的文件
1.用法:将js内容拷到一文件中,命名为batchRename.js: 该文件可以放到任何你想更改文件名的文件夹目录,然后dos(或 linux 终端)进入该文件夹,然后执行node batchRe ...
- CTF—攻防练习之HTTP—SQl注入(get)
攻击机:192.168.32.152 靶机 :192.168.32.157 扫描靶机扫端口: 开放了ssh和80看下ssh版本有没有漏洞,searchsplot下,没有发现 dirb扫描下目录,有个a ...
- c++ 指针(三)
指针 (6)传递指针给函数 只需要简单地声明函数参数为指针类型即可 #include <iostream> #include <ctime> using namespace s ...
- LeetCode.997-找到镇法官(Find the Town Judge)
这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘 ...
- Opencv之LBP特征(算法)
LBP(Local Binary Pattern),即局部二进制模式,对一个像素点以半径r画一个圈,在圈上取K个点(一般为8),这K个点的值(像素值大于中心点为1,否则为0)组成K位二进制数.此即局部 ...
- TP5报错Fatal error: require(): Failed opening required '/home/www/xx/public/../thinkphp/start.php
https://jingyan.baidu.com/article/afd8f4deb784fe34e386e97b.html https://www.cnblogs.com/300js/p/9224 ...
- zabbix监控java
参考: 官网: https://www.zabbix.com/documentation/4.0/manual/config/items/itemtypes/jmx_monitoring
- spring boot-12.Servlet 容器
1.spring boot 默认使用的是嵌入式的Servlet容器,spring-boot-starter-web 依赖了spring-boot-satrter-tomcat就是引入了嵌入式的tomc ...
- 2019CCPC厦门游记
距离上次2018CCPC吉林打铁一年有余,这次的厦门也是我们team拿到的第一块区域赛牌子,写一篇博客留念一下QAQ. 作为弱校的菜鸡队,我们提前两天就来到厦门,不得不说刚到厦门的两天还是很快乐的,住 ...