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

LeetCode //C - 20. Valid Parentheses

20. Valid Parentheses

Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
  3. Every close bracket has a corresponding open bracket of the same type.
     

Example 1:

Input: s = “()”
Output: true

Example 2:

Input: s = “()[]{}”
Output: true

Example 3:

Input: s = “(]”
Output: false

Constraints:

  • 1 < = s . l e n g t h < = 1 0 4 1 <= s.length <= 10^4 1<=s.length<=104
  • s consists of parentheses only ‘()[]{}’.

From: LeetCode
Link: 20. Valid Parentheses


Solution:

Ideas:

To determine if the string contains valid parentheses, we can use a stack. Here’s the approach:

  1. Create an empty stack.
  2. Traverse the string one character at a time.
  3. For each character:
  • If it’s an opening bracket ((, {, or [), push it onto the stack.
  • If it’s a closing bracket (), }, or ]), check if the stack is empty. If it’s empty, then there’s no matching opening bracket, so return false.
  • If the stack is not empty, pop the top element and check if it matches the corresponding opening bracket for the current closing bracket. If not, return false.
  1. After processing all characters in the string, check if the stack is empty. If it’s empty, then all opening brackets have been matched, so return true. Otherwise, return false.
Code:
bool isValid(char * s) {int n = strlen(s);char *stack = (char *)malloc(n * sizeof(char));int top = -1;  // stack is empty initiallyfor (int i = 0; i < n; i++) {if (s[i] == '(' || s[i] == '{' || s[i] == '[') {// Push the character onto the stackstack[++top] = s[i];} else {// Check for matching opening bracketif (top == -1) {// No opening bracket to matchfree(stack);return false;}char openBracket = stack[top--];  // Pop the top of the stackif (s[i] == ')' && openBracket != '(') {free(stack);return false;}if (s[i] == '}' && openBracket != '{') {free(stack);return false;}if (s[i] == ']' && openBracket != '[') {free(stack);return false;}}}bool result = (top == -1);free(stack);return result;
}
http://www.lryc.cn/news/133056.html

相关文章:

  • 浅析Java设计模式之四策略模式
  • 基于Spring Boot的餐厅订餐网站的设计与实现(Java+spring boot+MySQL)
  • 【图像分割】理论篇(1)评估指标代码实现
  • Git checkout 某个版本到指定文件夹下
  • Java多态详解(2)
  • Camtasia导入srt字幕乱码
  • YOLOv5、YOLOv8改进:SOCA注意力机制
  • 机器人的运动范围
  • 学习笔记|基于Delay实现的LED闪烁|模块化编程|SOS求救灯光|STC32G单片机视频开发教程(冲哥)|第六集(下):实现LED闪烁
  • 微服务-Ribbon(负载均衡)
  • 解决C#报“MSB3088 未能读取状态文件*.csprojAssemblyReference.cache“问题
  • GeoScene Pro在地图制图当中的应用
  • 国标混凝土结构设计规范的混凝土本构关系——基于python代码生成
  • 系统架构设计-架构师之路(八)
  • 【SA8295P 源码分析】25 - QNX Ethernet MAC 驱动 之 emac_isr_thread_handler 中断处理函数源码分析
  • 函数栈帧的创建与销毁
  • 工业安全生产平台在面粉行业的应用分享
  • Gitlab服务部署及应用
  • 【nodejs】用Node.js实现简单的壁纸网站爬虫
  • xlsx xlsx-style file-saver 导出json数据到excel文件并设置标题字体加粗
  • Win11游戏高性能模式怎么开
  • 深度学习最强奠基作ResNet《Deep Residual Learning for Image Recognition》论文解读(上篇)
  • 第22次CCF计算机软件能力认证
  • Go语言基础之基本数据类型
  • Linux Tracing Technologies
  • iOS自定义下拉刷新控件
  • Springboot写单元测试
  • 一篇文章教你使用Docker本地化部署Chatgpt(非api,速度非常快!!!)及裸连GPT的方式(告别镜像GPT)
  • 前馈神经网络dropout实例
  • Android DataStore:安全存储和轻松管理数据