leetcode 678. Valid Parenthesis String
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:
- Any left parenthesis
'('must have a corresponding right parenthesis')'. - Any right parenthesis
')'must have a corresponding left parenthesis'('. - Left parenthesis
'('must go before the corresponding right parenthesis')'. '*'could be treated as a single right parenthesis')'or a single left parenthesis'('or an empty string.- An empty string is also valid.
Example 1:
Input: "()"
Output: True
Example 2:
Input: "(*)"
Output: True
Example 3:
Input: "(*))"
Output: True
Note:
- The string size will be in the range [1, 100].
大致思路是:自左向右遍历时, 记录当前最多能匹配多少右括号(high表示),至少还要匹配多少右括号(low表示)。因为high的数值对后面的右括号还起作用,要维护这一变量,当某一时刻low<0时。说明右括号对于左侧过多。*号能根据后面右括号的情况而变化,遍历过程中只要low>=0,就行了。当遍历到一个左括号是,标志着它能用于抵消后面的右括号,也标志着,后面必须要有右括号或*号与其抵消。
类似只有'(' 和 ')' 的情况,count分别加减1,看是否最终等于0. 这里存在*情况,所以变为[low, high]
时间复杂度O(n),空间复杂度O(1).
class Solution {
public:
bool checkValidString(string s) {
if (s.size() == ){
return true;
}
int low = , high = ;
for (int i = ; i < s.length(); i++) {
if (s[i] == '(') {
low++;
high++;
} else if (s[i] == ')') {
if (low > ) {
low--;
}
high--;
} else {
if (low > ) {
low--;
}
high++;
}
if (high < ) {
return false;
}
}
return low == ;
}
};
leetcode 678. Valid Parenthesis String的更多相关文章
- [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】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 678. Valid Parenthesis String
https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是).是(的前提是:右边 ...
- 【leetcode】Valid Parenthesis String
题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...
- [LeetCode] 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 ...
- [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
随机推荐
- Harbor---docker镜像仓库搭建
在用docker,kubernetes时registry是重要的一环,下面就来搭建一个镜像仓库.之前在公司时有资源域名,有做ssl证书,今天没条件就不做ssl证书和域名了. 1,下载 harbor 安 ...
- 学python走过的坑 三 不能实现的浏览器缩放功能
公司一个项目,在启动web页面时,默认应该是打开项目页面,然后浏览器启动时总是打开一个广告页面,经理让写一个脚本,让电脑每次开机自启浏览器,且加载项目页面.浏览器自启和打开项目页面轻松搞定,这时问题来 ...
- 【Linux篇】--awk的使用
一.前述 awk是一个强大的文本分析工具.相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,(空格,制表符)为默认分隔符将每行切片 ...
- 文本离散表示(二):新闻语料的one-hot编码
上一篇博客介绍了文本离散表示的one-hot.TF-IDF和n-gram方法,在这篇文章里,我做了一个对新闻文本进行one-hot编码的小实践. 文本的one-hot相对而言比较简单,我用了两种方法, ...
- 『练手』001 Laura.SqlForever架构基础(Laura.XtraFramework 的变迁)
001 Laura.SqlForever架构的基础(Laura.XtraFramework 的变迁之路) Laura.XtraFramework 到底是 做什么的? Laura.XtraFramewo ...
- 接口自动化:HttpClient + TestNG + Java(二) - 第一个接口测试:get请求
在上一篇中,我们搭建好了HttpClient + TestNG + Java的自动化接口测试环境,这一篇我们就赶紧开始编写我们的第一个接口测试用例. 本篇会对问题解决的思路进行更详尽的阐述. 2.1 ...
- 分布式事务解决方案FESCAR
项目地址:FESCAR 以下是官网的文档.简介2019年,Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 1. ...
- VIM新手福利,配置向
今天搜索VIM配置的时候,搜到一个神器 https://github.com/amix/vimrc The Ultimate vimrc Over the last 10 years, I have ...
- chrome主页被篡改为hao123 win10系统
应该是开了个从流氓网站下的蓝灯,然后发现主页被篡改 尝试chrome设置修改无效,应该是快捷方式被改了 系统 win10 1.打开对应的下面两个地址,找到chrome的快捷方式,右键属性 C:\Use ...
- 分布式系统唯一ID的生成方案讨论
在分布式系统下唯一id问题,就是id咋生成?比如分表分库,因为要是一个表分成多个表之后,每个表的id都是从1开始累加自增长,那是不对的.举个例子,一个表拆分为了2张表,每个表的id都从1开始累加,这个 ...