首页 > 试题广场 >

栈的压入、弹出序列

[编程题]栈的压入、弹出序列
  • 热度指数:757714 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
1. 0<=pushV.length == popV.length <=1000
2. -1000<=pushV[i]<=1000
3. pushV 的所有数字均不相同
示例1

输入

[1,2,3,4,5],[4,5,3,2,1]

输出

true

说明

可以通过push(1)=>push(2)=>push(3)=>push(4)=>pop()=>push(5)=>pop()=>pop()=>pop()=>pop()
这样的顺序得到[4,5,3,2,1]这个序列,返回true      
示例2

输入

[1,2,3,4,5],[4,3,5,1,2]

输出

false

说明

由于是[1,2,3,4,5]的压入顺序,[4,3,5,1,2]的弹出顺序,要求4,3,5必须在1,2前压入,且1,2不能弹出,但是这样压入的顺序,1又不能在2之前弹出,所以无法形成的,返回false      
推荐
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV.size() == 0) return false;
        vector<int> stack;
        for(int i = 0,j = 0 ;i < pushV.size();){
            stack.push_back(pushV[i++]);
            while(j < popV.size() && stack.back() == popV[j]){
                stack.pop_back();
                j++;
            }       
        }
        return stack.empty();
    }
};

编辑于 2015-08-12 17:04:16 回复(173)
 int top=0;
int a[100000];
void push (int x);
void pop(int x);
int Check(int k);
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    int i,k=0;
    for(i=0;i<pushVLen;i++)
    {
        push(pushV[i]);
        if (pushV[i]==popV[k])
        {
            pop (pushV[i]);
            k++;
            while (1)
            {
                if (Check(popV[k])==0)
                    break;
                k++;
            }
        }
    }
    printf("%d",k);
    if (k==popVLen)
        return true;
    else return false;
}
void push (int x)
{
    top++;
    a[top]=x;
}
void pop(int x)
{
    top--;
}
int Check(int k)
{
    if (top<1)
        return 0;
    else {
        if (a[top]==k)
        {
            top--;
            return 1;
        }
        else return 0;

    }
    
}

编辑于 2024-01-13 13:09:11 回复(0)
typedef struct lnode {
    int data;
    struct lnode* next;
} node, *stack;

void Push(stack* top, int x) {
    stack cur = (stack)malloc(sizeof(node));
    cur->data = x;
    cur->next = *top;
    *top = cur;
}

void Pop(stack* top) {
    if (*top == NULL) {
        return; // Handle underflow if needed
    }
    *top = (*top)->next;
}

bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen) {
    stack s=NULL;
    int j = 0;
    for (int i = 0; i < pushVLen; i++) {
        Push(&s, pushV[i]);
        while (s != NULL && s->data == popV[j] && j < popVLen) {
            Pop(&s);
            j++;
        }
    }
    return (s == NULL && j == popVLen);
}

发表于 2023-11-22 02:50:18 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pushV int整型一维数组 
 * @param pushVLen int pushV数组长度
 * @param popV int整型一维数组 
 * @param popVLen int popV数组长度
 * @return bool布尔型
 */
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    // write code here
    int temp[pushVLen],top = 0;
    int i = 0,j = 0;
    while(i<pushVLen)
    {
        temp[top++] = pushV[i++];
        while(top>0&&j<popVLen&&temp[top-1]==popV[j])
        {
            j++;
            top--;
        }
    }
    return top==0;
}

发表于 2023-08-03 20:18:44 回复(0)
我是不是想复杂了?我咋觉得我这个复杂度是O(n2)呢,哎
#include <stdbool.h>
#include <stdio.h>
#define INVALID -1001
int FindIdx(int* pushV, int pushVLen, int val) {
    for (int i = 0; i < pushVLen; i++) {
        if (pushV[i] == val) {
            pushV[i] = INVALID;
            return i;
        }
    }
    return -1;
}
bool FindToLeft(int* pushV, int pushVLen, int* idx,int val) {
    if (*idx - 1 < 0)
        return false;
    if (pushV[*idx - 1] != INVALID && pushV[*idx - 1] == val)
     {
        *idx = *idx - 1;
		pushV[*idx] = INVALID;
        return true;
    }
    int i = *idx - 2;
     if (i < 0)
        return false;
    for (i = *idx - 2; i >= 0; i--) {
        if (pushV[i] != INVALID)
           break;
    }
    if (i >= 0 && pushV[*idx - 1] == INVALID &&pushV[i] == val)
    {
        *idx = i;
        pushV[i] = INVALID;
        return true;
    }
    return false;   
}
bool FindToRight(int* pushV, int pushVLen, int* idx,int val) {
    if(*idx+1>=pushVLen)
        return false;
    for (int i = *idx +1; i < pushVLen; i++) {
        if (pushV[i] != INVALID && pushV[i] == val)
        {
            *idx = i;
            pushV[i] = INVALID;
            return true;
        }
    }
    return false;
}
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    // write code here
    int idx = -1;
    idx = FindIdx(pushV, pushVLen, popV[0]);
     if(idx == -1)
            return false;
    for (int i = 1; i < popVLen; i++) {
        if (!FindToLeft(pushV, pushVLen, &idx,popV[i]) && 
            !FindToRight(pushV, pushVLen, &idx,popV[i]))
            return false;
    }
    return true;
}


发表于 2023-06-09 01:23:55 回复(1)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pushV int整型一维数组 
 * @param pushVLen int pushV数组长度
 * @param popV int整型一维数组 
 * @param popVLen int popV数组长度
 * @return bool布尔型
 */
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    // write code here
    int temp[pushVLen],top = -1;
    int i = 0,j = 0;
    while(i<pushVLen)
    {
        temp[++top] = pushV[i++];
        while(top>-1&&j<popVLen&&temp[top]==popV[j])
        {
            j++;
            top--;
        }
    }
    if(i==pushVLen&&j==popVLen)
        return true;
    else return false;
}


发表于 2023-05-21 21:31:01 回复(0)
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    // write code here
    int stack[1000], top = 0, *p1 = pushV, *p2 = popV;
    while (p1 < pushV + pushVLen) {
        while(*p1 != *p2) {
            stack[top++] = *p1;
            p1++;
        }
        if (*p1 == *p2)
            p2++, p1++;
        while(top > 0 && stack[top - 1] == *p2) {
            top--, p2++;
        }
    }
    if (top < 1)
        return true;
    else
        return false;
}
有什么问题吗?兄弟们,百思不得其解
发表于 2023-03-19 16:06:10 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pushV int整型一维数组
 * @param pushVLen int pushV数组长度
 * @param popV int整型一维数组
 * @param popVLen int popV数组长度
 * @return bool布尔型
 */

typedef struct {
    int* data;
    int top;
    int maxlen;
} stack, *stackp;
stackp stack_create(int len);
int stack_push(stackp s, int data);
int stack_pop(stackp s);
int stack_top(stackp s);

bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    int i, j = 0;
    stackp s;
    s = stack_create(pushVLen);

    for (i = 0; i < pushVLen; i++) {
        stack_push(s, pushV[i]);    //压栈
        while (stack_top(s) == popV[j] && j < popVLen && s->top != -1) { //当栈定元素和出栈表j指向元素相等时,循环多次判断出栈
            stack_pop(s);
            j++;
        }
    }
    if (j == popVLen)   //最后,如果出栈表出完,则出栈表可行
        return true;
    else
        return false;
}


stackp stack_create(int len) {
    stackp s;
    if ((s = (stackp)malloc(sizeof(stack))) == NULL) {
        puts("malloc failed !");
        return NULL;
    }
    if ((s->data = (int*)malloc(len * sizeof(int))) == NULL) {
        puts("malloc failed !");
        return NULL;
    }
    s->maxlen = len;
    s->top = -1;
    return s;
}

int stack_push(stackp s, int data) {
    if (s->top == s->maxlen - 1) {
        puts("stack is full");
        return -1;
    }
    s->top++;
    s->data[s->top] = data;
    return 1;
}

int stack_pop(stackp s) {
    if (s->top == -1) {
        puts("stack is empty");
        return 0;
    }
    int tmp;
    tmp = s->data[s->top];
    s->top--;
    return tmp;
}

int stack_top(stackp s) {
    if (s->top == -1) {
        puts("stack is empty");
        return 0;
    }
    return s->data[s->top];
}

















发表于 2023-01-02 23:38:18 回复(0)
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) 
{
    int stack[1001]={1001};
    int base=0,top=1;
    int i=0,j=0,m=0;
    for(i=0;i<pushVLen;i++)//初始化成1001
        stack[i]=1001;
    for(i=0;i<popVLen;i++)
    {
        if(stack[top-1]==popV[i])//和栈顶相不相等,相等出栈
        {
            top--;
            stack[top]=1001;
        }
        else//不相等,去压入栈找
        {
            if(m<pushVLen)//压入栈还没有找完
            {
                for(j=m;j<pushVLen;j++)
                {
                    if(pushV[j]==popV[i])//相等,停止
                        break;
                    else
                        stack[top++]=pushV[j];//不等,入栈
                }
            }
            else//压入栈已经找完了
                return false;
            
            if(j==pushVLen)//压入栈从m开始找到最后一个都没有找到和popV[i]相等的,返回false
                return false;
            else//在压入栈里找到了和popV[i]相等的
                m=j+1;
        }
    }
    return true;
    // write code here
}

发表于 2022-10-31 21:24:55 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pushV int整型一维数组
 * @param pushVLen int pushV数组长度
 * @param popV int整型一维数组
 * @param popVLen int popV数组长度
 * @return bool布尔型
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
#include <stdbool.h>
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    // write code here
    if (pushVLen != popVLen)return 0;
    int sort = 0;
    int V = 0;
    int* arr = (int*)malloc(pushVLen * sizeof(int));
    for (int j = 0; j < pushVLen; j++ ) {
        arr[sort] = pushV[j];
        if (arr[sort] != popV[V]) {
            sort++;
        } 
        else {
            while ((sort != -1) && (arr[sort] == popV[V])) {
                sort--;
                V++;
            }
            sort++;
        }
    }
    if (sort == 0) {
        return 1;
    } 
    else {
        return 0;
    }
}
发表于 2022-04-05 21:01:18 回复(0)