面试题22栈的压入弹出序列

本文介绍了一个算法,用于判断给定的两个整数序列是否能够通过一个栈实现从压入到弹出的操作。通过使用辅助栈模拟操作过程,验证了弹出序列的有效性。

剑指offer-面试题22.栈的压入,弹出序列

题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1、2、3、4、5是某栈的压栈序列,序列4、5、3、2、1是该压栈序列对应的一个弹出序列,但4、3、5、1、2就不可能是该该压栈序列的弹出序列。

本题的题解步骤如下:

1.设置一个辅助栈S,用于模拟出栈入栈顺序,设入栈顺序序列为pPush,出栈顺序序列为pPop

2.设置两个索引或指针分别指向入栈序列和出栈序列的第一个元素

3.顺序索引入栈元素,当入栈元素不等于出栈元素的时候,将入栈元素依次压入辅助栈S

4.当两者相等的时候,压入该元素到辅助栈S中同时将栈顶元素出栈,出栈入栈序列的索引均向后移动一个位置

5.当入栈序列索引结束之后,pPush剩余的元素全部已压入栈S

6.依次索引pPop如果pPop与辅助栈顶元素比较如果相等这将辅助栈顶弹出

7.当栈中所有的元素均弹出的时候则说明出栈顺序序列与正好是入栈顺序序列对应的出栈顺序。否则出栈顺序与入栈顺序不匹配

#include<iostream>
#include<stack>
using namespace std;
//pPush插入队列的顺序
//pPop弹出的顺序
//length   pPush的长度
bool IsPop(const int *pPush,const int *pPop,int length)
{
	bool possible=false;
	if(pPush!=NULL&&pPop!=NULL&&length>0)
	{
		const int* nextpush=pPush;
		const int* nextpop=pPop;
		std::stack<int>data;
		while(nextpop-pPop<length)
		{
			while(data.empty()||data.top()!=*nextpop)
			{
				if(nextpush-pPush==length)
					break;
				data.push(*nextpush);
				nextpush++;
			}
			if(data.top()!=*nextpop)
				break;
			data.pop();
			nextpop++;
		}
		if(data.empty() && nextpop-pPop==length)
			possible=true;
	}

	return possible;
}
void Test(char *testName,const int* pPush,const int*pPop,int nLength,bool expected)
{
	if(testName!=NULL)
		printf("%s begin: ",testName);
	if(IsPop(pPush,pPop,nLength)==expected)
		printf("passed\n");
	else
		printf("failed\n");
}
  
void Test1()  
{  
    const int nLength = 5;  
    int push[nLength] = {1, 2, 3, 4, 5};  
    int pop[nLength] = {4, 5, 3, 2, 1};  
      
    Test("Test1", push, pop, nLength, true);  
}  
  
void Test2()  
{  
    const int nLength = 5;  
    int push[nLength] = {1, 2, 3, 4, 5};  
    int pop[nLength] = {3, 5, 4, 2, 1};  
      
    Test("Test2", push, pop, nLength, true);  
}  
void Test3()  
{  
    const int nLength = 5;  
    int push[nLength] = {1, 2, 3, 4, 5};  
    int pop[nLength] = {4, 3, 5, 1, 2};  
      
    Test("Test3", push, pop, nLength, false);  
}  
  
void Test4()  
{  
    const int nLength = 5;  
    int push[nLength] = {1, 2, 3, 4, 5};  
    int pop[nLength] = {3, 5, 4, 1, 2};  
      
    Test("Test4", push, pop, nLength, false);  
}  
  
// push和pop序列只有一个数字  
void Test5()  
{  
    const int nLength = 1;  
    int push[nLength] = {1};  
    int pop[nLength] = {2};  
  
    Test("Test5", push, pop, nLength, false);  
}  
  
void Test6()  
{  
    const int nLength = 1;  
    int push[nLength] = {1};  
    int pop[nLength] = {1};  
  
    Test("Test6", push, pop, nLength, true);  
}  
  
void Test7()  
{  
    Test("Test7", NULL, NULL, 0, false);  
}  
   
int main()  
{  
    Test1();  
    Test2();  
    Test3();  
    Test4();  
    Test5();  
    Test6();  
    Test7();  
    system("pause");
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值