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的更多相关文章

  1. leetcode 678. Valid Parenthesis String

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

  2. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

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

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

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

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

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

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

  6. [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String

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

  7. LeetCode Valid Parenthesis String

    原题链接在这里:https://leetcode.com/problems/valid-parenthesis-string/description/ 题目: Given a string conta ...

  8. 【leetcode】Valid Parenthesis String

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

  9. How to convert any valid date string to a DateTime.

    DateTimeFormatInfo pattern = new DateTimeFormatInfo() { ShortDatePattern = "your date pattern&q ...

随机推荐

  1. ElasticSearch安装(以Docker的方式)

    拉取docker镜像: docker pull docker.elastic.co/elasticsearch/elasticsearch:6.1.1 命令行方式启动 测试环境     :docker ...

  2. Web前端开发的学习过程

    2018年 5月27日 开始在MDN上学习HTML/CSS/JavaScript.——6月18日 基本学完MDN的“学习Web开发”的HTML/CSS/JavaScript部分. 6月9日 开始在IF ...

  3. maven 配置Hibernate

    1.首先在maven中添加jar包依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId&g ...

  4. Today is the first day of the rest of your life.

    Today is the first day of the rest of your life. 今天是你余下人生的第一天.

  5. Cocos2d-x v3.1 Hello world程序(四)

    Cocos2d-x v3.1 Hello world程序(四) 在上一篇文章中我们我们已经使用Cocos-Console工具生成了工程,本机生成的目录为:"D:\CocosProject\T ...

  6. 关于配置httpd2.4.18+php5.6

    关于httpd2.4.18下载之前一直很烦php官网上的点半天看不到下载链接,直到看到这么几句话 大意是Apache http server 不提供二进制版本,只提供源代码.....如果你不能自己编译 ...

  7. Mantis-1.3.3 (Ubuntu 16.04)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mantis-1.3.3 bug tracking commercial devops mantis open-source project man ...

  8. SqlServer查询文件组被占用情况

    在SqlServer中,删除一个文件组 alter database [xxxxx] remove filegroup FGMonthTurnIntroduceByMonth13 有时候会遇到如下报错 ...

  9. OpenSSL context 的几个参数

    NAME SYNOPSIS DESCRIPTION NOTES BUGS RETURN VALUES EXAMPLES SEE ALSO NAME SSL_CTX_set_verify, SSL_se ...

  10. Google Guava入门(一)

    Guava作为Java编程的助手,可以提升开发效率,对Guava设计思想的学习则极大的有益于今后的编程之路.故在此对<Getting Started with Google Guava>一 ...