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 ...
随机推荐
- .NET Core微服务 权限系统+工作流(二)工作流系统
一.前言 接上一篇 .NET Core微服务 权限系统+工作流(一)权限系统 ,再来一发 工作流,我在接触这块开发的时候一直好奇它的实现方式,翻看各种工作流引擎代码,探究其实现方式,个人总结出来一个核 ...
- Shell笔试题2
1. 从a.log文件中提取包含"WARNING"或"FATAL",同时不包含"IGNOR"的行,然后提取以":"分割的 ...
- 前端专业术语: shim 和 Polyfill,了解下
在学习和使用 JavaScript 的时候,我们会经常碰到两个术语:shim 和 polyfill.它们有许多定义和解释,意思相近又有差异. Shim Shim 指的是在一个旧的环境中模拟出一个新 A ...
- xml 文件转化Dictionary
下面是xml文件 <?xml version="1.0" encoding="utf-8" ?><nodes> <国土局> ...
- The eighth day
time n(名词):时间:次,时代,时刻: vt(及物动词):为...安排时间:测定...的时间:调准(机械的速度): vi(不及物动词):合拍,和谐,打拍子 files (原型是fly) vi(不 ...
- CSS中box-sizing属性的作用
今天在项目中看到box-sizing这个属性,以前用过,但是不常用!注意,它是CSS3里的属性喔! W3C 盒子模型:标准盒模型,是指块元素box-sizing属性为content-box的盒模型.一 ...
- C# sftp通过秘钥上传下载
一.适用场景 我们平时习惯了使用ftp来上传下载文件,尤其是很多Linux环境下,我们一般都会通过第三方的SSH工具连接到Linux,但是当我们需要传输文件到Linux服务器当中,很多人习惯用ftp来 ...
- python 函数学习之sys.argv[1]
一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...
- COGS 201. [BYVoid S1] 埃雷萨拉斯的宝藏
★★ 输入文件:eldrethalas.in 输出文件:eldrethalas.out 简单对比时间限制:1 s 内存限制:256 MB 问题描述 一万两千年前,精灵还是在艾萨拉女王的 ...
- linux 命令——61 wget(转)
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...