0
点赞
收藏
分享

微信扫一扫

《剑指offer-P28》【剑指Offer 31.栈的压入、弹出序列】

禾木瞎写 2022-05-05 阅读 71

文章目录


💎一、题目

🏆1.题目描述

🏆2.原题链接

💎二、解题报告

🏆1.思路分析

🏆2.代码详解

bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
    if(pushed == NULL || popped == NULL || pushedSize != poppedSize)    //1
    {
        return false;
    }
    int *Stack = (int*)malloc(sizeof(int)*pushedSize);                  //2
    int top = -1, index = 0;
    for(int i = 0; i < pushedSize; ++i){                                //3
        Stack[++top] = pushed[i];
        while(top>-1 && Stack[top] == popped[index]){
            top--;
            index++;
        }
    }
    return top == -1;
}


举报

相关推荐

0 条评论