678. Valid Parenthesis String
https://leetcode.com/problems/valid-parenthesis-string/description/
这个题的难点在增加了*,*可能是(也可能是)。是(的前提是:右边有多余的)。是)的前提是:左边有多余的(。所以他们的位置信息是重要的。
class Solution {
public:
bool checkValidString(string s) {
// *可以是(或者), 用openMax/Min来表示最多和最少可能没有被匹配的(的数目.
// openMin是由(引起的,openMax是由(和*引起的。)可以和openMax/Min匹配。
int openMax = , openMin = ;
for (auto c : s) {
if (c == '(') { // 如果是(,openMax/Min都要加1
openMax++;
openMin++;
}
else if (c == ')') {
if (openMax < ) return false; // 左边未匹配的(最多也没有1个,错误
openMax--;
if (openMin > ) // 如果openMin是0,说明匹配的是前面的*。
openMin--;
}
else {
openMax++; // 可以作为(的数目+1.
if (openMin > ) // 如果之前有未匹配的(,这个*可以匹配那个(.
openMin--;
}
}
return openMin == ; // 如果openMin是0,说明(都被匹配了
}
};
Valid Parenthesis通常可以用stack来做。这个题目一个stack做不了,考虑用2个?
class Solution {
public:
bool checkValidString(string s) {
stack<int> left, star;
for (int i = ; i < s.length(); i++) {
if (s[i] == '(')
left.push(i);
else if (s[i] == ')') {
if (!left.empty())
left.pop();
else if (!star.empty())
star.pop();
else
return false;
}
else
star.push(i);
}
while (!left.empty()) {
if (star.empty() || star.top() < left.top())
return false;
left.pop();
star.pop();
}
return true;
}
};
678. Valid Parenthesis String的更多相关文章
- 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]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] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- LeetCode Valid Parenthesis String
原题链接在这里:https://leetcode.com/problems/valid-parenthesis-string/description/ 题目: Given a string conta ...
- 【leetcode】Valid Parenthesis String
题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...
- How to convert any valid date string to a DateTime.
DateTimeFormatInfo pattern = new DateTimeFormatInfo() { ShortDatePattern = "your date pattern&q ...
随机推荐
- Hadoop源生实用工具之distcp
1 概览 DistCp(Distributed Copy)是用于大规模集群内部或者集群之间的高性能拷贝工具. 它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成. 它把文件和目录的列 ...
- pat1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 根据FileUpload的值,控制textBox的可用与否
JS代码: <script type="text/javascript"> $(document).ready(function () { $("#<% ...
- 检查python以及django是否安装配置成功
首先说明下,我使用pycharm作为开发的IDE,在第一次创建django项目的时候,会自动安装django包的.(网上也有很多单独安装的方法),环境变量配置成功后,就是用下面的方法检测安装成功与否. ...
- Ubuntu上安装Samba服务器实现家庭共享
如何在Ubuntu上安装Samba服务器 大多数Linux发行版都包含Samba. 要在Ubuntu上安装Samba,只需运行: sudo apt install samba 要检查您的Samba版本 ...
- hibernate课程 初探单表映射1-11 通过hibernate API访问编写第一个小例子
hibernate 业务流程 1 创建配置对象 Configuration config = new Configuration().configure(); 2 创建服务注册对象 Service ...
- a href="javascript:"与a href="#"
<a href="javascript:;"></a> <a href="#"></a> 这两种写法.这两种写法 ...
- Hibernate笔记3--多表操作-导航查询
一.一对多操作 1.构造实体类及编写配置文件: 一方: // 一个Customer对应多个linkman private Set<Linkman> linkmans = new ...
- [转]git修改远程仓库地址
原文链接:http://www.cnblogs.com/lazb/articles/5597878.html 问:Coding远程仓库地址变了,本地git仓库地址如何更新为最新地址 git修改远程仓库 ...
- GIT教程笔记
GIT的工作流程: 先在工作目录中添加.修改文件 一般是在工作目录建立你的工程文件夹,然后通过命令行进入文件夹后 git init 初始化 将需要进行版本管理的文件放入缓存区 git add 文件 ...