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. 将MySQL转化为mysqli

    <?php/** * Created by PhpStorm. * User: 大神 * Date: 2017/7/24 * Time: 11:29 */ header('content-typ ...

  2. <Linux系统isosize指令用法>

    isosize命令:iso9660文件系统大小显示 isosize命令用于显示iso9660文件系统的大小,还文件可以使普通文件,也可以是块设备,如/dev/sr0或者/dev/sda.如果没有相关选 ...

  3. JS语法字典---网友总结

    1.document.write(""); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个浏 ...

  4. Java hibernate 遇到的问题:could not read a hi value

    问题: 解决办法:在网上看到一篇文章说是把数据库实体类的注解@GeneratedValue改成@GeneratedValue(strategy = GenerationType.IDENTITY) , ...

  5. Spring scope 配置

    Scope 描述的是 Spring 容器如何新建Bean的实例,Spring的Scope有以下几种,通过@Scope来注解实现: 1. Singleton: 一个Spring容器中只有一个Bean的实 ...

  6. 用python计算直角三角形斜边长

    直接上代码 import math def hypotenuse(a,b): return(math.sqrt(a**2+b**2)) side1 = int(input("第一条直角边:& ...

  7. Vue日历

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

  8. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:0.概述

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 序言 帖主和队友仿制了一个简单版的微信,其中,队友是用Unity3D做前段,帖主用Java的Mina.Hiberna ...

  9. VS找到了XXX的副本,但是当前源代码与XXX中内置的版本不同

    1.博客园有解决ASP.Net出现以上问题的方法: 删除ASP.Net临时文件夹内的dll文件. https://www.cnblogs.com/autumn/p/5261576.html 2.但我的 ...

  10. C#之linq

    本文根据30分钟LINQ教程学习作的笔记. 1.Guid.Empty Guid 结构: 表示全局唯一标识符 (GUID).Empty字段:Guid 结构的只读实例,其值均为零.用来设置初始值.   G ...