• LeetCode 热题100——栈与队列专题(三)


    一、有效的括号

    20.有效的括号(题目链接

    思路:

    1)括号的顺序匹配:用栈实现,遇到左括号入,遇到右括号出(保证所出的左括号与右括号对应),否则顺序不匹配。

    2)括号的数量匹配:

    1>左括号大于右括号:用栈实现,遇到左括号入,遇到右括号出,遍历完字符数组,此时栈不为空,则说明左括号数量大于右括号;

    2>右括号大于左括号:遇到右括号出时,判断栈是否为空,若此时栈为空,说明右括号数量大于左括号;

    1. typedef char SDateType;
    2. typedef struct Stack
    3. {
    4. SDateType* a;
    5. int top;
    6. int capacity;
    7. }Stack;
    8. //初始化栈和销毁栈
    9. void InitStack(Stack* ps)
    10. {
    11. assert(ps);
    12. ps->a = NULL;
    13. ps->capacity = ps->top = 0;
    14. }
    15. void DestoryStack(Stack* ps)
    16. {
    17. assert(ps);
    18. free(ps->a);
    19. ps->a = NULL;
    20. ps->capacity = ps->top = 0;
    21. }
    22. //出栈和入栈
    23. void StackPush(Stack* ps, SDateType x)
    24. {
    25. assert(ps);
    26. //扩容
    27. if (ps->top == ps->capacity)
    28. {
    29. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    30. SDateType* tmp = (SDateType*)realloc( ps->a,newcapacity * sizeof(SDateType));
    31. if (tmp == NULL)
    32. {
    33. perror("realloc fail:");
    34. return;
    35. }
    36. ps->a = tmp;
    37. ps->capacity = newcapacity;
    38. }
    39. //尾插
    40. ps->a[ps->top] = x;
    41. ps->top++;
    42. }
    43. void StackPop(Stack* ps)
    44. {
    45. assert(ps);
    46. assert(ps->top > 0);//只少有一个元素,才能删除
    47. ps->top--;
    48. }
    49. //栈的有效个数和栈顶元素
    50. int StackSize(Stack* ps)
    51. {
    52. assert(ps);
    53. return ps->top;
    54. }
    55. int StackTop(Stack* ps)
    56. {
    57. assert(ps);
    58. assert(ps->top > 0);
    59. return ps->a[ps->top - 1];
    60. }
    61. //栈是否为空
    62. bool StackEmpty(Stack* ps)
    63. {
    64. assert(ps);
    65. return ps->top == 0;
    66. }
    67. int isValid(char* s) {
    68. Stack ps;
    69. InitStack(&ps);
    70. while (*s)
    71. {
    72. if (*s == '[' || *s == '(' || *s == '{')
    73. {
    74. StackPush(&ps, *s);
    75. s++;
    76. }
    77. else
    78. {
    79. if (StackEmpty(&ps))
    80. {
    81. return false;
    82. }
    83. char tmp = StackTop(&ps);
    84. StackPop(&ps);
    85. if ((*s == ']' && tmp != '[') ||
    86. (*s == '}' && tmp != '{') ||
    87. (*s == ')' && tmp != '('))
    88. {
    89. return false;
    90. }
    91. s++;
    92. }
    93. }
    94. if (!StackEmpty(&ps))
    95. {
    96. return false;
    97. }
    98. else {
    99. return true;
    100. }
    101. DestoryStack(&ps);
    102. }

    二、用队列实现栈 

    225. 用队列复制栈(题目链接)

     

    思路: 栈是后进先出,队列是先进先出。

    用俩队列实现栈

    1)入栈时,选择有数据的队列入数据;

    2)出栈时,将有数据队列中前k-1个数据出队列,并入到空队列中,返回并出有数据队列的最后一个数据

    1. typedef int QDateType;
    2. typedef struct QueueNode
    3. {
    4. QDateType val;
    5. struct QueueNode * next;
    6. }QueueNode;
    7. typedef struct Queue
    8. {
    9. QueueNode *head;
    10. QueueNode *tail;
    11. QDateType size;
    12. }Queue;
    13. // 初始化队列
    14. void QueueInit(Queue* pq)
    15. {
    16. assert(pq);
    17. pq->head = pq->tail = NULL;
    18. pq->size = 0;
    19. }
    20. void QueuePush(Queue* pq, QDateType x)
    21. {
    22. assert(pq);
    23. QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    24. node->val = x;
    25. node->next = NULL;
    26. if (pq->tail == NULL)
    27. {
    28. pq->head = pq->tail = node;
    29. pq->size++;
    30. }
    31. else
    32. {
    33. pq->tail->next = node;
    34. pq->tail = node;
    35. pq->size++;
    36. }
    37. }
    38. void QueuePop(Queue* pq)
    39. {
    40. assert(pq);
    41. assert(pq->head != NULL);//只少保证一个节点
    42. QueueNode* del = pq->head;
    43. pq->head = pq->head->next;
    44. free(del);
    45. del = NULL;
    46. pq->size--;
    47. if (pq->head == NULL)//只有一个节点处理
    48. {
    49. pq->tail = NULL;
    50. }
    51. }
    52. // 返回队头和队尾
    53. QDateType QueueFront(Queue* pq)
    54. {
    55. assert(pq);
    56. return pq->head->val;
    57. }
    58. QDateType QueueBack(Queue* pq)
    59. {
    60. assert(pq);
    61. return pq->tail->val;
    62. }
    63. // 获取队列中有效元素个数
    64. int QueueSize(Queue* pq)
    65. {
    66. assert(pq);
    67. return pq->size;
    68. }
    69. bool QueueEmpty(Queue* pq)
    70. {
    71. assert(pq);
    72. return pq->head==NULL;
    73. }
    74. void QueueDestroy(Queue* pq)
    75. {
    76. assert(pq);
    77. QueueNode* cur = pq->head;
    78. while (cur)
    79. {
    80. QueueNode* next = cur->next;
    81. free(cur);
    82. cur = next;
    83. }
    84. pq->head = pq->tail = NULL;
    85. pq->size = 0;
    86. }
    87. typedef struct {
    88. Queue q1;
    89. Queue q2;
    90. } MyStack;
    91. MyStack* myStackCreate() {
    92. MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
    93. QueueInit(&obj->q1);
    94. QueueInit(&obj->q2);
    95. return obj;
    96. }
    97. void myStackPush(MyStack* obj, int x) {
    98. if(!QueueEmpty(&obj->q1))
    99. {
    100. QueuePush(&obj->q1,x);
    101. }
    102. else{
    103. QueuePush(&obj->q2,x);
    104. }
    105. }
    106. bool myStackEmpty(MyStack* obj) {
    107. return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
    108. }
    109. int myStackPop(MyStack* obj) {
    110. assert(!myStackEmpty(obj));
    111. Queue * empty=&obj->q1;
    112. Queue * nonempty=&obj->q2;
    113. if(!QueueEmpty(empty))
    114. {
    115. empty=&obj->q2;
    116. nonempty=&obj->q1;
    117. }
    118. while(QueueSize(nonempty)!=1)
    119. {
    120. int tmp= QueueFront(nonempty);
    121. QueuePush(empty,tmp);
    122. QueuePop(nonempty);
    123. }
    124. int tmp= QueueFront(nonempty);
    125. QueuePop(nonempty);
    126. return tmp;
    127. }
    128. int myStackTop(MyStack* obj) {
    129. assert(!myStackEmpty(obj));
    130. Queue * empty=&obj->q1;
    131. Queue * nonempty=&obj->q2;
    132. if(!QueueEmpty(empty))
    133. {
    134. empty=&obj->q2;
    135. nonempty=&obj->q1;
    136. }
    137. return QueueBack(nonempty);
    138. }
    139. void myStackFree(MyStack* obj) {
    140. QueueDestroy(&obj->q1);
    141. QueueDestroy(&obj->q2);
    142. free(obj);
    143. }

    三、用栈实现队列 

    225. 用栈实现队列(题目链接)

    思路: 栈是后进先出,队列是先进先出。

    1)入队列:s1栈用来在栈顶入数据;

    2)出队列:s2栈用来出栈顶数据,如果s2为空,则从s1出数据入到s2中去(比如;s1中的数据为1,2,3,入到s2变为3,2,1),再出s2的栈顶数据,这样就满足队列的性质了

     

    1. typedef int SDateType;
    2. typedef struct Stack
    3. {
    4. SDateType* a;
    5. int top;
    6. int capacity;
    7. }Stack;
    8. //初始化栈和销毁栈
    9. void InitStack(Stack* ps)
    10. {
    11. assert(ps);
    12. ps->a = NULL;
    13. ps->capacity = ps->top = 0;
    14. }
    15. void DestoryStack(Stack* ps)
    16. {
    17. assert(ps);
    18. free(ps->a);
    19. ps->a = NULL;
    20. ps->capacity = ps->top = 0;
    21. }
    22. //出栈和入栈
    23. void StackPush(Stack* ps, SDateType x)
    24. {
    25. assert(ps);
    26. //扩容
    27. if (ps->top == ps->capacity)
    28. {
    29. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    30. SDateType* tmp = (SDateType*)realloc( ps->a,newcapacity * sizeof(SDateType));
    31. if (tmp == NULL)
    32. {
    33. perror("realloc fail:");
    34. return;
    35. }
    36. ps->a = tmp;
    37. ps->capacity = newcapacity;
    38. }
    39. //尾插
    40. ps->a[ps->top] = x;
    41. ps->top++;
    42. }
    43. void StackPop(Stack* ps)
    44. {
    45. assert(ps);
    46. assert(ps->top > 0);//只少有一个元素,才能删除
    47. ps->top--;
    48. }
    49. //栈的有效个数和栈顶元素
    50. int StackSize(Stack* ps)
    51. {
    52. assert(ps);
    53. return ps->top;
    54. }
    55. int StackTop(Stack* ps)
    56. {
    57. assert(ps);
    58. assert(ps->top > 0);
    59. return ps->a[ps->top - 1];
    60. }
    61. //栈是否为空
    62. bool StackEmpty(Stack* ps)
    63. {
    64. assert(ps);
    65. return ps->top == 0;
    66. }
    67. typedef struct {
    68. Stack s1;
    69. Stack s2;
    70. } MyQueue;
    71. MyQueue* myQueueCreate() {
    72. MyQueue* obj=(MyQueue* )malloc(sizeof(MyQueue));
    73. InitStack(&obj->s1);
    74. InitStack(&obj->s2);
    75. return obj;
    76. }
    77. void myQueuePush(MyQueue* obj, int x) {
    78. StackPush(&obj->s1,x);
    79. }
    80. bool myQueueEmpty(MyQueue* obj) {
    81. return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2);
    82. }
    83. int myQueuePop(MyQueue* obj) {
    84. assert(!myQueueEmpty(obj));
    85. if(StackEmpty(&obj->s2))
    86. {
    87. while(!StackEmpty(&obj->s1))
    88. {
    89. int tmp= StackTop(&obj->s1);
    90. StackPush(&obj->s2,tmp);
    91. StackPop(&obj->s1);
    92. }
    93. }
    94. int tmp= StackTop(&obj->s2);
    95. StackPop(&obj->s2);
    96. return tmp;
    97. }
    98. int myQueuePeek(MyQueue* obj) {
    99. assert(!myQueueEmpty(obj));
    100. if(StackEmpty(&obj->s2))
    101. {
    102. while(!StackEmpty(&obj->s1))
    103. {
    104. int tmp= StackTop(&obj->s1);
    105. StackPush(&obj->s2,tmp);
    106. StackPop(&obj->s1);
    107. }
    108. }
    109. return StackTop(&obj->s2);
    110. }
    111. void myQueueFree(MyQueue* obj) {
    112. DestoryStack(&obj->s1);
    113. DestoryStack(&obj->s2);
    114. free(obj);
    115. }

     四、设计循环队列

    622.设计循环队列(题目链接) 

    思路一:数组

    以front为队列头下标,tail为队列尾下一个元素下标,一共k个数据,开辟k+1个整型大小空间,方便区分队列为空、为满以及一个元素的情况

    1)队列为空,front=tail

    2)队列为1个元素,front+1=tail

    3)   队列为满,(tail+1)%(k+1)==front

     

    1. typedef struct {
    2. int *a;
    3. int front;
    4. int rear;
    5. int n;
    6. } MyCircularQueue;
    7. MyCircularQueue* myCircularQueueCreate(int k) {
    8. MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    9. int * tmp=(int *)malloc(sizeof(int)*(k+1));
    10. obj->a=tmp;
    11. obj->front=0;
    12. obj->rear=0;
    13. obj->n=k;
    14. return obj;
    15. }
    16. bool myCircularQueueIsFull(MyCircularQueue* obj) {
    17. if((obj->rear+1)%(obj->n+1)==obj->front)
    18. {
    19. return true;
    20. }
    21. return false;
    22. }
    23. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    24. if(myCircularQueueIsFull( obj))
    25. {
    26. return false;
    27. }
    28. obj->a[obj->rear]=value;
    29. obj->rear++;
    30. obj->rear=obj->rear%(obj->n+1);
    31. return true;
    32. }
    33. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    34. if(obj->front==obj->rear)
    35. {
    36. return true;
    37. }
    38. return false;
    39. }
    40. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    41. if(myCircularQueueIsEmpty( obj))
    42. {
    43. return false;
    44. }
    45. obj->front++;
    46. obj->front=obj->front%(obj->n+1);
    47. return true;
    48. }
    49. int myCircularQueueFront(MyCircularQueue* obj) {
    50. if(myCircularQueueIsEmpty( obj))
    51. {
    52. return -1;
    53. }
    54. return obj->a[obj->front];
    55. }
    56. int myCircularQueueRear(MyCircularQueue* obj) {
    57. if(myCircularQueueIsEmpty( obj))
    58. {
    59. return -1;
    60. }
    61. return obj->a[(obj->rear-1+obj->n+1)%(obj->n+1)];
    62. }
    63. void myCircularQueueFree(MyCircularQueue* obj) {
    64. free(obj->a);
    65. free(obj);
    66. }

     

    思路二:单向循环链表

    以head为队列头节点,tail为队列尾尾节点的下一个节点,一共k个数据,开辟k+1个节点的循环单向链表,方便区分队列为空、为满以及一个元素的情况

    1)队列为空,head=tail

    2)队列为1个元素,head->next=tail

    3)   队列为满,tail->next=head

     

    1. typedef struct QueueNode
    2. {
    3. int val;
    4. struct QueueNode * next;
    5. }QueueNode;
    6. QueueNode* BuyNode(int x)
    7. {
    8. QueueNode* node=(QueueNode*)malloc(sizeof(QueueNode));
    9. node->val=x;
    10. node->next=NULL;
    11. return node;
    12. }
    13. typedef struct MyCircularQueue{
    14. QueueNode *head;
    15. QueueNode *tail;
    16. QueueNode * pretail;
    17. int n;
    18. } MyCircularQueue;
    19. MyCircularQueue* myCircularQueueCreate(int k) {
    20. MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    21. QueueNode* node=BuyNode(0);
    22. obj->pretail=NULL;
    23. obj->head=obj->tail=node;
    24. obj->n=(k+1);
    25. QueueNode* cur=obj->tail;
    26. while(k--)
    27. {
    28. QueueNode* node=BuyNode(0);
    29. cur->next=node;
    30. cur= cur->next;
    31. }
    32. cur->next=obj->head;
    33. return obj;
    34. }
    35. bool myCircularQueueIsFull(MyCircularQueue* obj) {
    36. if(obj->tail->next==obj->head)
    37. {
    38. return true;
    39. }
    40. return false;
    41. }
    42. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    43. if(myCircularQueueIsFull( obj))
    44. {
    45. return false;
    46. }
    47. obj->tail->val=value;
    48. obj->pretail=obj->tail;
    49. obj->tail=obj->tail->next;
    50. return true;
    51. }
    52. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    53. if(obj->head==obj->tail)
    54. {
    55. return true;
    56. }
    57. return false;
    58. }
    59. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    60. if(myCircularQueueIsEmpty( obj))
    61. {
    62. return false;
    63. }
    64. obj->head=obj->head->next;
    65. return true;
    66. }
    67. int myCircularQueueFront(MyCircularQueue* obj) {
    68. if(myCircularQueueIsEmpty( obj))
    69. {
    70. return -1;
    71. }
    72. return obj->head->val;
    73. }
    74. int myCircularQueueRear(MyCircularQueue* obj) {
    75. if(myCircularQueueIsEmpty( obj))
    76. {
    77. return -1;
    78. }
    79. return obj->pretail->val;
    80. }
    81. void myCircularQueueFree(MyCircularQueue* obj) {
    82. while(obj->n--)
    83. {
    84. QueueNode*next=obj->head->next;
    85. free(obj->head);
    86. obj->head=next;
    87. }
    88. obj->head=NULL;
    89. free(obj);
    90. }

     

  • 相关阅读:
    String的compareTo()方法使用场景介绍及全量ASCII 码表(完整版)
    P2404 自然数的拆分问题
    vue项目中的 env文件从何而来?什么是 process.env
    九、MySQL之视图的介绍
    OpenStack学习笔记-Glance组件深入了解
    黑马点评-发布探店笔记
    【C++】匿名对象 ③ ( 函数返回值为对象值时 匿名对象 的 拷贝构造函数 与 析构函数 调用情况分析 )
    TypeScript学习 | 泛型
    中国石油大学(北京)-《 油气藏经营管理》第二阶段在线作业
    单词规律(C++解法)
  • 原文地址:https://blog.csdn.net/zhoubancheng/article/details/134538488