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. java实现定时任务(Quartz)

    java中实现定时任务执行某一业务.具体操作如下: 1.定义初始化任务 2.任务业务操作 3.定义初始化方法 4.在web.xml中注册启动 5.定义具体执行时间 6.quartz定时任务时间设置 q ...

  2. ABAP:从例子学习ABAP

    1.插入内表行: *插入内表行: DATA: BEGIN OF man, name(20) TYPE c, high TYPE p DECIMALS 2, weight TYPE p DECIMALS ...

  3. 碎碎念css

    块状元素单独占一行,但加上float变成跟着别人,有空就插!float让块级元素变行内元素

  4. 原生ajax瀑布流demo

    最近听朋友们说起瀑布流挺多的,自己就去研究下了,一个简单的原生demo,分享给大家... 简单分为三个文档,有详细的注释:img:ajax.php:demo.php 其中img文件夹中放入图片 1.j ...

  5. Vue日历

    Vue生成日历,根据返回值将日期标红 HTML: <h1>CSS 日历</h1> <div id="calendar"> <div cla ...

  6. Android 在已有工程中实现微信图片压缩

    这个我们需要自己去编译,但是已经有人帮我们编译好了,压缩算法也已经实现,因此,我们去下载然后编译即可:https://github.com/bither/bither-android-lib 首先将上 ...

  7. 详情介绍win7:编辑文件夹时提示操作无法完成,因为其中的文件夹或文件已在另一个程序中打开的解决过程

    我们在使用电脑中,总会遇到下面这种情况: 那怎么解决呢,现在就开始教程: 在电脑的底下显示各种图标那一行点击右键,再选择“启动任务管理器” 接下来你就可以对你刚刚要操作的文件进行重命名.删除等操作啦! ...

  8. File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录)

    File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录) 以前的两篇文章(收录在书本「上集」的第十七章) 请看「ASP.NET专题实务」,松岗出版 File 与 Log ...

  9. iOS 7系列译文:认识 TextKit

    OS 7:终于来了,TextKit.   功能   所以咱们到了.iOS7 带着 TextKit 登陆了.咱们看看它可以做什么!深入之前,我还想提一下,严格来说,这些事情中的大部分以前都可以做.如果你 ...

  10. iOS内存探秘

    http://www.cocoachina.com/ios/20170216/18689.html iOS 内存机制特点 有限的可用内存 iPhone 设备的 RAM 一直非常紧缺,iPhone 一代 ...