[leetcode]678. 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:
- Any left parenthesis
'('
must have a corresponding right parenthesis')'
. - Any right parenthesis
')'
must have a corresponding left parenthesis'('
. - Left parenthesis
'('
must go before the corresponding right parenthesis')'
. '*'
could be treated as a single right parenthesis')'
or a single left parenthesis'('
or an empty string.- An empty string is also valid.
Example 1:
- Input: "()"
- Output: True
Example 2:
- Input: "(*)"
- Output: True
Example 3:
- Input: "(*))"
- Output: True
题目
验证有效括号字符串
思路
recursion
类似Leetcode 22 Generate Parenthesis 思路
代码
- class Solution {
- public boolean checkValidString(String s) {
- return check(s, 0, 0);
- }
- private boolean check(String s, int start, int count) {
- if (count < 0) return false;
- for (int i = start; i < s.length(); i++) {
- char c = s.charAt(i);
- if (c == '(') {
- count++;
- }
- else if (c == ')') {
- if (count <= 0) return false;
- count--;
- }
- else if (c == '*') {
- //1. * for '(' --> (*))
- //2. * for ')' --> ((*)
- //3. * for empty string --> (*)
- return check(s, i + 1, count + 1) || check(s, i + 1, count - 1) || check(s, i + 1, count);
- }
- }
- return count == 0;
- }
- }
[leetcode]678. Valid Parenthesis String验证有效括号字符串的更多相关文章
- [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
678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- 678. Valid Parenthesis String
https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 【leetcode】Valid Parenthesis String
题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...
- LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
随机推荐
- unity admob
插件地址:https://github.com/unity-plugins/Unity-Admob 2017.04.11测试使用发现GoogleMobileAds.framework有问题,导致出现U ...
- Spring STS Call Hierarchy 查找不到被调用的信息
今天使用Spring的STS的时候,发现Call Hierarchy无法使用,很奇怪,发现问题出现在同一个工作区间里,如果工作区间不在此工作区间,发现还是可以找到被调用的信息的.当时在网上找也没找到 ...
- mysql 忘记密码解决方案
Mysql 忘记root密码的完美解决方法 转载 2016-12-23 作者:MR.QiGao 我要评论 通常在使用Mysql数据库时,如果长时间没有登陆,或者由于工作交接完成度不高,会导 ...
- 13.BeanUtils组件-基础.md
目录 用途 基本属性的设置 Map数据的拷贝 对象的拷贝 转换器 用途 可以用来对JavaBean的各种增强操作 基本属性的设置 package per.liyue.code.beanutildemo ...
- Uni2D 入门 -- Atlas转载 http://blog.csdn.net/kakashi8841/article/details/17588095
转载csdnTexture Atlas 我为什么应该使用Texture Atlas? 使用Atlas是一个普遍的好做法,而且它有很多好处.当有某些需要在屏幕渲染的时候,它背后带来的是draw call ...
- 吴裕雄 python深度学习与实践(2)
#coding = utf8 import threading,time,random count = 0 class MyThread (threading.Thread): def __init_ ...
- python 自然语言处理库https://www.nltk.org/nltk_data/
https://www.nltk.org/nltk_data/ https://github.com/hankcs/HanLP
- 11.14java课堂测试
源代码: import java.*; import java.util.*; import java.io.File; import java.io.FileInputStream; import ...
- 渲染Keynote
[渲染Keynote] 1.渲染图元(rendering primitives),可以是点.线.三角. 2.显卡对于显存的访问速度更快,而且大多数显卡对于RAM没有直接的访问权利 . 3.裁剪(Cli ...
- msf客户端渗透(四):关闭UAC,hash、防火墙、服务、磁盘、DEP、防病毒、远程桌面、桌面截图、获取tooken
关闭UAC 如果你获得一个system权限的session 进入到这个session 进入到shell 依次输入以下命令 cmd.exe /k %windir%\System32\reg.exe AD ...