• leetcode:20. 有效的括号


    一、题目:

    链接:20. 有效的括号 - 力扣(LeetCode)

     

    函数原型:bool isValid(char* s)

    二、思路:

    利用栈来解这道题会方便许多:

    遍历字符串s,当遇到左括号就将其压入栈中;遇到右括号首先判断栈是否为空,如果为空说明左右括号数量不匹配返回false,再判断它与左括号是否匹配,如果不匹配返回false,如果匹配则将栈顶元素出栈。全部遍历完成后,如果栈不为空,说明左右括号数量不匹配,返回false,如果栈为空则返回true。

    三、代码:

    1. //顺序栈的结构定义
    2. typedef char STDataType;
    3. typedef struct Stack {
    4. STDataType* a;
    5. int top;
    6. int capacity;
    7. }ST;
    8. //顺序栈的初始化
    9. void STInit(ST* pst)
    10. {
    11. assert(pst);
    12. pst->a = NULL;
    13. pst->top = 0;//top指针指向栈顶元素的下一位
    14. pst->capacity = 0;//顺序栈的容量
    15. }
    16. //顺序栈的打印
    17. void STPrint(ST pst)
    18. {
    19. for (int i = 0; i < pst.top; i++)
    20. {
    21. printf("%d ", pst.a[i]);
    22. }
    23. printf("\n");
    24. }
    25. //顺序栈的入栈
    26. void STPush(ST* pst, STDataType x)
    27. {
    28. assert(pst);
    29. //检查扩容
    30. if (pst->top == pst->capacity)
    31. {
    32. int newcapacity = pst->capacity == 0 ? 4 : 2 * pst->capacity;
    33. STDataType* p = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
    34. if (p == NULL)
    35. {
    36. perror("realloc fail");
    37. exit(-1);
    38. }
    39. else
    40. {
    41. pst->a = p;
    42. pst->capacity = newcapacity;
    43. }
    44. }
    45. pst->a[pst->top++] = x;
    46. }
    47. //顺序栈出栈
    48. void STPop(ST* pst)
    49. {
    50. assert(pst);
    51. assert(pst->top > 0);
    52. pst->top--;
    53. }
    54. //求顺序栈栈顶元素
    55. STDataType STTop(ST* pst)
    56. {
    57. assert(pst);
    58. assert(pst->top > 0);
    59. return pst->a[pst->top - 1];
    60. }
    61. //顺序栈判空
    62. bool STEmpty(ST* pst)
    63. {
    64. assert(pst);
    65. return pst->top == 0;
    66. }
    67. //顺序栈销毁
    68. void STDestroy(ST* pst)
    69. {
    70. assert(pst);
    71. if (pst->a != NULL)
    72. {
    73. free(pst->a);
    74. pst->a = NULL;
    75. pst->top = 0;
    76. pst->capacity = 0;
    77. }
    78. }
    79. bool isValid(char* s) {
    80. //定义顺序栈
    81. ST st;
    82. //初始化顺序栈
    83. STInit(&st);
    84. int len=strlen(s);
    85. for(int i=0;i
    86. {
    87. if(s[i]=='('||s[i]=='{'||s[i]=='[')
    88. {
    89. STPush(&st,s[i]);
    90. }
    91. else if(s[i]==')'||s[i]=='}'||s[i]==']')
    92. {
    93. if(STEmpty(&st))
    94. return false;
    95. else
    96. {
    97. if((STTop(&st)=='('&&s[i]!=')')||(STTop(&st)=='{'&&s[i]!='}')||(STTop(&st)=='['&&s[i]!=']'))
    98. return false;
    99. STPop(&st);
    100. }
    101. }
    102. }
    103. if(!STEmpty(&st))
    104. return false;
    105. else
    106. return true;
    107. }
  • 相关阅读:
    Flink Checkpoint
    记一次 .NET 某供应链WEB网站 CPU 爆高事故分析
    Docker Desktop安装以及MYSQL, GRAFANA安装
    【无标题】
    【LeetCode】No.98. Validate Binary Search Tree -- Java Version
    理解JavaScript事件循环机制
    DOM 基础操作
    Linux Kafka 3.5 KRaft模式集群部署
    微服务篇-C 深入理解第一代微服务(SpringCloud)_II 深入理解Eureka服务治理
    详解 TCP 原理
  • 原文地址:https://blog.csdn.net/2301_76197086/article/details/134543113