LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)
题目:
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
分析:
括号匹配,左括号和右括号必须对应上,且左括号在右括号前面,*号可以当任意的左括号右括号或者是空。
首先可以利用动态规划,求解dp[i][j],其中i,j表示字符串的字串范围,为1表示可以匹配上,0表示不能匹配上。当i等于j是,也就是一个字符,只有当时‘*’的时候才能匹配,那如果s[i]是*或者是左括号,s[j]是*或者右括号,且dp[i+1][j-1]是成功匹配的,那么dp[i][j]也是匹配的。否则就要在i到j之间寻找一个分割点k使得i到k,k+1到j之间的字符串是匹配的。
那还可以利用计数法,设置两个变量,一个记录当前需要匹配的最小左括号数,和一个当前需要匹配最大左括号数。
遍历字符串,当前字符为左括号时,意味着我们必须要匹配一个左括号,所以最小左括号数+1,其他情况最小左括号数减一,因为*可以当成右括号,消掉一个左括号。
当前字符为右括号时,意味着我们必须要匹配一个右括号,此时最大左括号数-1,其他情况最大左括号数+1,因为*可以当成左括号,增加一个最大的左括号数。
当最大左括号数小于0时,代表已经有右括号没有相应的左括号或者*和它匹配了,如果最小左括号数小于0,则重新将它置为0,因为最小左括号数小于0的情况时发生在有多个*出现,我们把*当成空,所以置其为0.最后如果最小括号数为0的话,就代表匹配成功了。
程序:
C++
class Solution {
public:
bool checkValidString(string s) {
int n = s.length();
if(n == 0) return true;
dp = vector<vector<int>>(n, vector<int>(n, -1));
return checkValid(s, 0, n-1);
}
private:
vector<vector<int>> dp;
bool checkValid(string& s, int i, int j){
if(i > j)
return true;
if(dp[i][j] >= 0)
return dp[i][j];
if(i == j){
if(s[i] == '*'){
return dp[i][j] = 1;
}
else{
return dp[i][j] = 0;
}
}
if((s[i] == '*' || s[i] == '(') && (s[j] == '*' || s[j] == ')') && checkValid(s, i+1, j-1)){
return dp[i][j] = 1;
}
for(int k = i; k < j; ++k){
if(checkValid(s, i, k) && checkValid(s, k+1, j)){
return dp[i][j] = 1;
}
}
return dp[i][j] = 0;
}
};
Java
class Solution {
public boolean checkValidString(String s) {
char[] str = s.toCharArray();
if(str.length == 0) return true;
int min_op = 0;
int max_op = 0;
for(char ch:str){
if(ch == '('){
min_op++;
}else{
min_op--;
}
if(ch == ')'){
max_op--;
}else{
max_op++;
}
if (max_op < 0) return false;
min_op = Math.max(0, min_op);
}
if(min_op == 0)
return true;
else
return false;
}
}
LeetCode 678. Valid Parenthesis String 有效的括号字符串 (C++/Java)的更多相关文章
- [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
678. Valid Parenthesis String Medium Given a string containing only three types of characters: '(', ...
- [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] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- [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 ...
随机推荐
- ClkLog自定义事件分析登场
ClkLog的自定义事件分析功能在大家满满的期待下终于发布了. 这次更新我们添加了[用户关联].[事件采集].[事件分析]三大块功能点. 本次上线的自定义事件分析可以让用户根据自身业务场景创建不同维 ...
- PolarDB-X 全局Binlog解读之性能篇(上)
简介: 本篇来介绍一下PolarDB-X全局binlog在性能方面的一些设计和思考,先通过几个实际的测试案例来展示全局binlog的性能情况,然后结合这些案例来深入讲解全局binlog关于优化的故事. ...
- 通过Jenkins构建CI/CD实现全链路灰度
简介: 本文介绍通过 Jenkins 构建流水线的方式实现全链路灰度功能. 作者:卜比 本文介绍通过 Jenkins 构建流水线的方式实现全链路灰度功能. 在发布过程中,为了整体稳定性,我们总是希 ...
- Apsara Stack 同行者专刊 | 政企混合云技术架构的演进和发展
简介: 现在,政企客户已进入到用云计算全面替换传统IT基础架构的攻坚阶段,混合云与传统架构的技术产品能力也正在经历全面的比较与评估.阿里云混合云平台首席架构师张晓丹分享IT架构技术深刻洞察,并对政企混 ...
- 如何使用 Serverless Devs 部署静态网站到函数计算(上)
简介:部署个静态网站到函数计算~ 前言 公司经常有一些网站需要发布上线,对比了几款不同的产品后,决定使用阿里云的函数计算(FC)来托管构建出来的静态网站. FC 弹性实例自带的500 Mb 存储空 ...
- 基于 Serverless 架构的头像漫画风处理小程序
简介: 当一个程序员想要个漫画风的头像时... 前言 我一直都想要有一个漫画版的头像,奈何手太笨,用了很多软件 "捏不出来",所以就在想着,是否可以基于 AI 实现这样一个功能, ...
- [GPT] jquery chosen插件选择的多个元素是逗号分隔的,怎么设置成其它分隔符号 ?
如果你想要在 jQuery Chosen 插件中使用其它分隔符号,可以通过以下方式实现: 1. 设置 delimiter 选项为一个包含所需分隔符的字符串. $(".chosen-selec ...
- 新闻网页Python爬虫(jieba分词+关键词搜索排序)
前言 最近做了一个python3作业题目,涉及到: 网页爬虫 网页中文文字提取 建立文字索引 关键词搜索 涉及到的库有: 爬虫库:requests 解析库:xpath 正则:re 分词库:jieba ...
- sqli-labs-master 导入导出 + 第七关
1.load_file()导出文件 load_file(file_name):读取文件并返回该文件内容作为一个字符串. 使用条件: A:必须有权限读取并且文件完全可读 B:预读取文件必修在服务器上 C ...
- docker / compose 的安装 和 体验
文档 官网文档 视频 视频 简介 课程内容 1.Docker Compose 容器编排 2.Docker Swarm #集群 热扩容 需要在阿里上买服务器,至少冲100+以上的人民币 文档: 集群方式 ...