有效的括号.leetcode20 《数据结构入门到精通N8》

简介: 有效的括号.leetcode20 《数据结构入门到精通N8》

题目链接


20. 有效的括号 - 力扣(LeetCode) (leetcode-cn.com)


题目描述


image.png

image.png

image.png



思路


用栈来解决这个问题。当为左括号时,入栈,为右括号时出栈,同时注意括号的个数是否匹配,如左括号多余右括号,或者右括号多余左括号的情况。(注意因为是用c语言来实现,所以我们还要单独来实现一个栈)



代码


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
//struct Stack
//{
//  int a[N];
//  int top; // 栈顶的位置
//};
typedef int STDataType;
typedef struct Stack
{
  STDataType* a;
  int top;    // 栈顶的位置
  int capacity; // 容量
}ST;
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);
STDataType StackTop(ST* ps);
void StackInit(ST* ps)
{
  assert(ps);
  ps->a = NULL;
  ps->top = 0;
  ps->capacity = 0;
}
void StackDestory(ST* ps)
{
  assert(ps);
  free(ps->a);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
  assert(ps);
  // 
  if (ps->top == ps->capacity)
  {
    int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    ps->a = (STDataType*)realloc(ps->a, newCapacity* sizeof(STDataType));
    if (ps->a == NULL)
    {
      printf("realloc fail\n");
      exit(-1);
    }
    ps->capacity = newCapacity;
  }
  ps->a[ps->top] = x;
  ps->top++;
}
void StackPop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  --ps->top;
}
bool StackEmpty(ST* ps)
{
  assert(ps);
  /*if (ps->top > 0)
  {
    return false;
  }
  else
  {
    return true;
  }*/
  return ps->top == 0;
}
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
  assert(ps);
  return ps->top;
}
bool isValid(char * s){
    ST st;
    StackInit(&st);
    while(*s)
    {
        if((*s=='(')||(*s=='[')||(*s=='{'))
        {
            //左括号就入栈   右括号就出栈
            StackPush(&st,*s);
            s++;
        }
        else
        {
            //如果需要出栈 但是已经没有可出的了,说明不匹配直接return
            //如: ()) 
            if(StackEmpty(&st))
            return false;
            //取栈顶
            char top=StackTop(&st);
            StackPop(&st);
            //因为继续的条件有很多,所以我们判断return的条件
            //这里很值得注意
            if((*s==')' && top!='(')
            ||(*s==']' && top!='[')
            ||(*s=='}' && top!='{'))
            {
                StackDestory(&st);
                return false;
            }
            s++;
        }
    }
    //如果栈此时不等于空,就说明左括号有多余的
    if(!StackEmpty(&st))
    {
        StackDestory(&st);
        return false;
    }
    //此时栈为空,说明前面的左右括号都匹配了
    else
    {
        StackDestory(&st);
        return true;
    }
}

最后的最后,创作不易,希望读者三连支持💖

赠人玫瑰,手有余香💖

相关文章
|
4天前
leetcode代码记录(有效的括号
leetcode代码记录(有效的括号
12 1
|
4天前
|
存储 数据库 C++
高效处理大规模数据集的概率型数据结构—— 布隆过滤器 [C++入门]
高效处理大规模数据集的概率型数据结构—— 布隆过滤器 [C++入门]
16 0
|
4天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
22 0
|
4天前
|
存储 机器学习/深度学习 算法
|
4天前
栈刷题记(一-有效的括号)
栈刷题记(一-有效的括号)
栈刷题记(一-有效的括号)
|
4天前
|
存储 NoSQL 算法
Redis入门到通关之Redis数据结构-Hash篇
Redis入门到通关之Redis数据结构-Hash篇
24 1
|
4天前
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-Set篇
Redis入门到通关之Redis数据结构-Set篇
20 1
|
4天前
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-ZSet篇
Redis入门到通关之Redis数据结构-ZSet篇
23 1
|
4天前
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-List篇
Redis入门到通关之Redis数据结构-List篇
33 1
|
4天前
|
存储 NoSQL 安全
Redis入门到通关之Redis数据结构-String篇
Redis入门到通关之Redis数据结构-String篇
35 1