946. Validate Stack Sequences C语言 fasater than 100%

bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize) {
    int top=0;
    int i,j,flag=1;
    i=j=0;
    int *stack=malloc(sizeof(int)*(pushedSize+2));
    stack[0]=0;
    while(i<poppedSize)
    {
        while(top==0||stack[top]!=popped[j]&&i<poppedSize)
        {
            stack[++top]=pushed[i++];
        }
        while(top!=0&&j<poppedSize&&stack[top]==popped[j])
        {
            top--;
            j++;
        }
    }
    if(j==poppedSize)
        return 1;
    else
        return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37372543/article/details/89021460