20.有效的括号(LeetCode)

简介: 20.有效的括号(LeetCode)

8668c9706876bb79c9a808c9743a125c_e78bcd051ec74d63bbce2a94c46c6f5f.png


思路:用栈的后进先出的特性,来完成题目的要求


8edd303faa4b495c8b01a62e8602c774.png


因为C++有库,可以直接用,而C语言没有,所以我们直接把写好的栈拷贝上来用。  


首先,完成框架的搭建


07bd535d2e6847ac86a24d767a85b37d.png


其次,再实现循环内的部分。1.左括号入栈 2.右括号出栈匹配


c0e71d1382564dc3b2e0df2309e769f3.png


这里在右括号匹配的判断,要注意不要写成两个都相等,这样不能说明全都匹配成功,所以就写成两边不相等,满足则直接return false,不满足则继续循环


每次循环结束,s++。所有循环停止后,没有return false,则return true


dbaa5900d5ee4e8d9f402e93579f9b46.png


看起来好像没有什么问题,对吧?


其实,上述只适用于左右括号数量相等的场景,我们还要考虑两种特殊情况 :


1.左括号多于右括号


2.右括号多于左括号


左括号多于右括号时,循环结束,栈内元素个数不为0,则用STEmpty判断一下 ,如果为空,与之前相同,返回true,如果不为空,则返回false


169eac13303d409a9a320118a95e5a6e.png


右括号多于左括号时,在循环内部,直到栈已经空了,还有右括号要匹配,那么此时也直接返回false


0498cbe549f336c351ac4fb07379abcd_df7c8614de6a43559f29eea3cc078322.png


完整代码如下:


typedef char STDataType;
typedef struct Stack
{
  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;//top指向栈顶元素的下一个位置
  pst->capacity = 0;
}
void STDestroy(ST* pst)
{
  assert(pst);
  free(pst->a);
  pst->top = pst->capacity = 0;
}
void STPush(ST* pst, STDataType x)
{
  assert(pst);
  if (pst->top == pst->capacity)
  {
  int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
  STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
  if (tmp == NULL)
  {
    perror("realloc fail");
    return;
  }
  pst->a = tmp;
  pst->capacity = newCapacity;
  }
  pst->a[pst->top++] = x;
}
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)
    {
        //1.左括号入栈
        //2.右括号出栈匹配
        if (*s == '('
        ||*s == '['
        ||*s == '{')
        {
            STPush(&st, *s);
        }
        else
        {
            //解决右括号多于左括号的问题
            if (STEmpty(&st))
            {
                STDestroy(&st);
                return false;
            }
            char top = STTop(&st);
            STPop(&st);
            if ((top != '(' && *s == ')')
            ||(top != '[' && *s == ']')
            ||(top != '{' && *s == '}'))
            {
                STDestroy(&st);
                return false;
            }
        }
        s++;
    }
    //解决左括号多于右括号的问题
    bool ret = STEmpty(&st);
    STDestroy(&st);
    return ret;
}
相关文章
|
2月前
|
存储 C语言 索引
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
|
4月前
leetcode-301:删除无效的括号
leetcode-301:删除无效的括号
19 0
LeetCode | 20. 有效的括号
LeetCode | 20. 有效的括号
|
3天前
leetcode代码记录(有效的括号
leetcode代码记录(有效的括号
9 1
|
2月前
|
算法 安全 Java
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
22 0
|
3月前
|
Java
|
3月前
LeetCode题 338比特位计数,20有效的括号,415字符串相加
LeetCode题 338比特位计数,20有效的括号,415字符串相加
36 0
|
4月前
leetcode:20. 有效的括号
leetcode:20. 有效的括号
13 0
|
4月前
leetcode-856:括号的分数
leetcode-856:括号的分数
17 0
|
4月前
leetcode-921:使括号有效的最少添加
leetcode-921:使括号有效的最少添加
21 0

热门文章

最新文章