力扣---LeetCode20. 有效的括号(栈)

简介: 第十二弹——力扣LeetCode每日一题

🌟一、20. 有效的括号


给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。有效字符串需满足:

左括号必须用相同类型的右括号闭合。

左括号必须以正确的顺序闭合

每个右括号都有一个对应的相同类型的左括号。

🌟二、链接


20. 有效的括号

🌟三、方法:栈实现


🌏3.1思路:


用栈来实现,如果是左括号(" [ " " { " " ( “)就入栈,如果是右括号(” ] " " } " " ) ")就出栈,然后取出栈中的top和右括号比较,如果匹配的上就是有效括号(用C语言实现是需要创建栈的)

不懂得宝贝可以看【数据结构】— 几分钟走进栈和队列(详解-上)

🌏3.2代码:


typedef char STDataType;
typedef struct Strack
{
  STDataType* a;
  int top;
  int capacity;
}ST;
void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
void STInit(ST* pst)
{
  assert(pst);
  pst->a = NULL;
  pst->top = 0;
  pst->capacity = 0;
}
void STDestroy(ST* pst)
{
  assert(pst);
  free(pst->a);
  pst->a = NULL;
  pst->capacity =pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
  if (pst->top == pst->capacity)
  {
    int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
    STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newCapacity);
    if (tmp == NULL)
    {
      perror("realloc fail");
      return;
    }
    pst->a = tmp;
    pst->capacity = newCapacity;
  }
  pst->a [pst->top ] = x;
  pst->top++;
}
void STPop(ST* pst)
{
  assert(pst);
  assert(!STEmpty(pst));
  pst->top--;
}
STDataType STTop(ST* pst)
{
  assert(pst);
  assert(!STEmpty(pst));
  return pst->a[pst->top-1 ];
}
bool STEmpty(ST* pst)
{
  assert(pst);
  return pst->top == 0;
}
int STSize(ST* pst)
{
  assert(pst);
  return pst->top;
}
bool isValid(char * s)
{
    ST st;
    STInit(&st);
    while(*s)
    {
        if(*s=='('||*s=='{'||*s=='[')//左括号进栈
        {
        STPush(&st,*s);//进栈
        }
        else
        {
            if(STEmpty(&st))
            //当为右括号没有左括号时,我们直接返回false"]"不然下面要取栈顶中的值,但是没有左括号,所以栈中为空,就会出现越界问题所以要判空一下判断栈中是否有左括号
            {
                STDestroy(&st);
                return false;
            }
            char top=STTop(&st);//取出栈顶元素
            STPop(&st);//然后将(栈顶元素)出栈
            if(top=='('&&*s!=')'||top=='['&&*s!=']'||top=='{'&&*s!='}')
            //两者不匹配时,就返回false
            {
                STDestroy(&st);
                return false;
            }
        }
        //不然就s++比较下一组
        s++;
    }
    bool ret= STEmpty(&st);//如果出现"[([ ])"这样一组匹配完栈中就会剩余”[“,所以还要对栈判空一下,如果有值bool就返回false,为空bool就返回ture
    //bool---波尔类型
    STDestroy(&st);
    return ret;
}

🌟四、补充:


bool是Bool ean的缩写,只有真(True)和假(False)两种取值bool函数只有一个参数,并根据这个参数的值返回真或者假。

1.当对数字使用bool函数时,0返回假(False),任何其他值都返回真。

😽总结


😽Ending,今天的力扣每日一题内容就到此结束啦,如果后续想了解更多,就请关注我吧。

相关文章
LeetCode 热题100——单调栈
LeetCode 热题100——单调栈
19 0
|
2月前
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
34 6
|
2月前
|
存储 C语言 索引
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
|
4月前
【Leetcode 2487】从链表中移除节点 —— 单调栈
解题思路:维持一个单调递增栈,当栈为空时,记录当前节点为头节点;否则当前节点为栈顶节点的后继节点
|
4月前
【Leetcode 1944】队列中可以看到的人数 —— 单调栈
解题思路:维持一个单调递增栈来辅助计算每个人能够看到的人数。从右往左遍历数组,对于每个人,我们将其身高与栈顶元素的身高进行比较。如果当前人的身高比栈顶元素的身高高,则栈顶元素无法被当前人看到,将其出栈,并累计计数
|
2天前
|
Java
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
【LeetCode力扣】面试题 17.14. 最小K个数(top-k问题)
8 1
LeetCode | 232. 用栈实现队列
LeetCode | 232. 用栈实现队列
|
2天前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
5 0
|
3天前
leetcode代码记录(有效的括号
leetcode代码记录(有效的括号
10 1
|
24天前
|
C语言
Leetcode每日一题——“用栈实现队列”
Leetcode每日一题——“用栈实现队列”

热门文章

最新文章