[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 ...
随机推荐
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 使用curator框架简单操作zookeeper 学习笔记
Curator 操作是zookeeper的优秀api(相对于原生api),满足大部分需求.而且是Fluent流式api风格. 参考文献:https://www.jianshu.com/p/70151f ...
- 使用 Python 把多个 MP4 合成一个视频(转)
这两天群里有个小伙伴有一个需求, 就是把很多个视频文件 合并成一个. 期间也找了各种软件, 如格式工厂, 但是只能一次合成50个文件, 小伙伴有几千个文件需要合成, 太繁琐; 又比如会声会影, 这个剪 ...
- c++复习:STL之理论基础
1 STL(标准模板库)理论基础 1.1基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之 ...
- Haskell语言学习笔记(75)Conduit
安装 conduit $ cabal install conduit Installed conduit-1.3.0.3 Prelude> import Conduit Prelude Cond ...
- python学习笔记之斐波拉契数列学习
著名的斐波拉契数列(Fibonacci),除第一个和第二个数外,任意一个数都可由前两个数相加得到: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 如果用Python的列表生成式, ...
- jenkins+docker+docker-compose完整发版流程
首先搭建jenkins+maven+nexus这一套自动化打包工具,并配置好相应配置,这里就不再赘述了. 其次,搭建好docker集群和私有仓库,以及安装好docker-compose工具,配置好相应 ...
- 编译Linux内核(Mac OS平台)
操作系统第一次实验需要编译Linux内核,我之前在Mac上一直使用的都是Parallels Desktop这个软件,所以这次也将课程网站上提供的Ubuntu安装在了PD上,但是编译完内核后无法进入Ub ...
- 11.mysql-权限.md
目录 -- ***********五.mysql权限问题**************** -- mysql数据库权限问题:root :拥有所有权限(可以干任何事情) -- 权限账户,只拥有部分权限(C ...
- ArcGIS案例学习笔记-聚类点的空间统计特征
ArcGIS案例学习笔记-聚类点的空间统计特征 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:对于聚集点,根据分组字段case field,计算空间统计特征 ...