当前位置: 首页 > news >正文

【Leetcode】【240407】678. Valid Parenthesis String

It’s time to go back home, today’s in tomorrow lol
BGM:无地自容(黑豹乐队《黑豹》)

Descripition

Given a string s containing only three types of characters: ‘(’, ‘)’ and ‘*’, return true if s is valid.

The following rules define a valid string:

  • 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 singleright parenthesis ‘)’ or a single left parenthesis ‘(’ or an empty string “”.

Solution

Caution: Maybe take care of the pair order of ‘*’? Plz feel free to use queue instead and combine with stack.

Code

class Solution {
public:bool checkValidString(string s){typedef struct pair{char chat;int num;};stack<pair>pat;queue<pair>star;for(int i=0;i<s.length();++i){pair mid;mid.chat=s[i];mid.num=i;if(s[i]=='(') pat.push(mid);else if(s[i]=='*') star.push(mid);else{if(pat.empty()&&star.empty()) return false;else if(!pat.empty()) pat.pop();else if(pat.empty()&&(!star.empty())) star.pop(); }}stack<pair>new_star;while(!star.empty()){new_star.push(star.front());star.pop();}while(!pat.empty()){pair left=pat.top();if(!new_star.empty()){pair right=new_star.top();if(right.num<left.num) return false;else{new_star.pop();pat.pop();}}else return false;}return true;}
};
http://www.lryc.cn/news/334405.html

相关文章:

  • 移动平台相关(安卓)
  • [C++][算法基础]食物链(并查集)
  • 深入理解Transformer的位置编码机制
  • 10分钟上手:MySQL8的Json格式字段使用总结干货
  • OpenCV 4.9基本绘图
  • 显示器and拓展坞PD底层协商
  • 如何利用Flutter将应用成功上架至iOS平台:详细指南
  • 【运输层】网络数据报协议 UDP
  • 数据结构(初阶):顺序表实战通讯录
  • Outlook会议邀请邮件在答复后就不见了
  • 【C++】list模拟实现
  • ETL工具-nifi干货系列 第八讲 处理器PutDatabaseRecord 写数据库(详细)
  • 【MySQL】如何判断一个数据库是否出问题
  • SQLite数据库的性能问题并不是单纯地由数据量的大小决定的,而是受到多种因素的综合影响。以下是一些可能导致SQLite性能问题的因素
  • Blender怎么样启动默认移动和Cavity效果
  • Android 解决TextView多行滑动与NestedScrollView嵌套滑动冲突的问题
  • Laravel 开发Api规范
  • 蓝色wordpress外贸建站模板
  • windos环境,使用docker容器运行项目的,新增外部访问地址配置
  • 设计模式:生活中的组合模式
  • WPF OnStartup
  • docker-相关
  • 二十、Rust AOP 切面增强
  • 掌握Go语言:Go语言精细错误,清晰、高效的错误处理实践(32)
  • Spring与Web环境的集成
  • 二叉树的遍历——bfs广度优先搜索
  • 飞鸟写作可靠吗 #职场发展#经验分享#经验分享
  • Java 实现自定义注解
  • 代码随想录Day48
  • Web 后台项目,权限如何定义、设置、使用:菜单权限、按钮权限 ts element-ui-Plus