程序员面试题精选100题(24)-栈的push、pop序列
2012-11-21 21:18
429 查看
程序员面试题精选100题(24)-栈的push、pop序列
题目:输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。
比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。因为可以有如下的push和pop序列:push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,这样得到的pop序列就是4、5、3、2、1。但序列4、3、5、1、2就不可能是push序列1、2、3、4、5的pop序列。
分析:这到题除了考查对栈这一基本数据结构的理解,还能考查我们的分析能力。
这道题的一个很直观的想法就是建立一个辅助栈,每次push的时候就把一个整数push进入这个辅助栈,同样需要pop的时候就把该栈的栈顶整数pop出来。
我们以前面的序列4、5、3、2、1为例。第一个希望被pop出来的数字是4,因此4需要先push到栈里面。由于push的顺序已经由push序列确定了,也就是在把4 push进栈之前,数字1,2,3都需要push到栈里面。此时栈里的包含4个数字,分别是1,2,3,4,其中4位于栈顶。把4 pop出栈后,剩下三个数字1,2,3。接下来希望被pop的是5,由于仍然不是栈顶数字,我们接着在push序列中4以后的数字中寻找。找到数字5后再一次push进栈,这个时候5就是位于栈顶,可以被pop出来。接下来希望被pop的三个数字是3,2,1。每次操作前都位于栈顶,直接pop即可。
再来看序列4、3、5、1、2。pop数字4的情况和前面一样。把4 pop出来之后,3位于栈顶,直接pop。接下来希望pop的数字是5,由于5不是栈顶数字,我们到push序列中没有被push进栈的数字中去搜索该数字,幸运的时候能够找到5,于是把5 push进入栈。此时pop 5之后,栈内包含两个数字1、2,其中2位于栈顶。这个时候希望pop的数字是1,由于不是栈顶数字,我们需要到push序列中还没有被push进栈的数字中去搜索该数字。但此时push序列中所有数字都已被push进入栈,因此该序列不可能是一个pop序列。
也就是说,如果我们希望pop的数字正好是栈顶数字,直接pop出栈即可;如果希望pop的数字目前不在栈顶,我们就到push序列中还没有被push到栈里的数字中去搜索这个数字,并把在它之前的所有数字都push进栈。如果所有的数字都被push进栈仍然没有找到这个数字,表明该序列不可能是一个pop序列。
基于前面的分析,我们可以写出如下的参考代码:
#include <stack>
/////////////////////////////////////////////////////////////////////////////
// Given a push order of a stack, determine whether an array is possible to
// be its corresponding pop order
// Input: pPush - an array of integers, the push order
// pPop - an array of integers, the pop order
// nLength - the length of pPush and pPop
// Output: If pPop is possible to be the pop order of pPush, return true.
// Otherwise return false
/////////////////////////////////////////////////////////////////////////////
bool IsPossiblePopOrder(const int*
pPush, const int* pPop, int nLength)
{
bool bPossible = false;
if(pPush && pPop && nLength > 0)
{
const int *pNextPush = pPush;
const int *pNextPop = pPop;
// ancillary stack
std::stack<int> stackData;
// check every integers in pPop
while(pNextPop - pPop < nLength)
{
// while the top of the ancillary stack is not the integer
// to be poped, try to push some integers into the stack
while(stackData.empty() || stackData.top() != *pNextPop)
{
// pNextPush == NULL means all integers have been
// pushed into the stack, can't push any longer
if(!pNextPush)
break;
stackData.push(*pNextPush);
// if there are integers left in pPush, move
// pNextPush forward, otherwise set it to be NULL
if(pNextPush - pPush < nLength - 1)
pNextPush ++;
else
pNextPush = NULL;
}
// After pushing, the top of stack is still not same as
// pPextPop, pPextPop is not in a pop sequence
// corresponding to pPush
if(stackData.top() != *pNextPop)
break;
// Check the next integer in pPop
stackData.pop();
pNextPop ++;
}
// if all integers in pPop have been check successfully,
// pPop is a pop sequence corresponding to pPush
if(stackData.empty() && pNextPop - pPop == nLength)
bPossible = true;
}
return bPossible;
}
测试程序:
#include<iostream>
#include<stack>
const int SIZE=5;
using namespace std;
bool judge(int Spush[],int Spop[])
{
stack<int> my_stack;
int iPush=0,iPop=0;
while(iPush<SIZE)
{
cout<<"push"<<Spush[iPush]<<endl;
my_stack.push(Spush[iPush]);
while(!my_stack.empty()&&iPop!=5&&my_stack.top()==Spop[iPop])
{
cout<<"Pop "<<Spop[iPop]<<endl;
iPop++;
my_stack.pop();
}
iPush++;
}
if(iPop==SIZE) return true;
else return false;
}
int main(void)
{
int Spush[SIZE],Spop[SIZE];
for(int i=0;i<SIZE;i++)
cin>>Spush[i];
for(int j=0;j<SIZE;j++)
cin>>Spop[i];
if(judge(Spush,Spop))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
system("pause");
return 0;
}
参考资料:<<剑指offer 作者:何海涛>>
题目:输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。
比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。因为可以有如下的push和pop序列:push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,这样得到的pop序列就是4、5、3、2、1。但序列4、3、5、1、2就不可能是push序列1、2、3、4、5的pop序列。
分析:这到题除了考查对栈这一基本数据结构的理解,还能考查我们的分析能力。
这道题的一个很直观的想法就是建立一个辅助栈,每次push的时候就把一个整数push进入这个辅助栈,同样需要pop的时候就把该栈的栈顶整数pop出来。
我们以前面的序列4、5、3、2、1为例。第一个希望被pop出来的数字是4,因此4需要先push到栈里面。由于push的顺序已经由push序列确定了,也就是在把4 push进栈之前,数字1,2,3都需要push到栈里面。此时栈里的包含4个数字,分别是1,2,3,4,其中4位于栈顶。把4 pop出栈后,剩下三个数字1,2,3。接下来希望被pop的是5,由于仍然不是栈顶数字,我们接着在push序列中4以后的数字中寻找。找到数字5后再一次push进栈,这个时候5就是位于栈顶,可以被pop出来。接下来希望被pop的三个数字是3,2,1。每次操作前都位于栈顶,直接pop即可。
再来看序列4、3、5、1、2。pop数字4的情况和前面一样。把4 pop出来之后,3位于栈顶,直接pop。接下来希望pop的数字是5,由于5不是栈顶数字,我们到push序列中没有被push进栈的数字中去搜索该数字,幸运的时候能够找到5,于是把5 push进入栈。此时pop 5之后,栈内包含两个数字1、2,其中2位于栈顶。这个时候希望pop的数字是1,由于不是栈顶数字,我们需要到push序列中还没有被push进栈的数字中去搜索该数字。但此时push序列中所有数字都已被push进入栈,因此该序列不可能是一个pop序列。
也就是说,如果我们希望pop的数字正好是栈顶数字,直接pop出栈即可;如果希望pop的数字目前不在栈顶,我们就到push序列中还没有被push到栈里的数字中去搜索这个数字,并把在它之前的所有数字都push进栈。如果所有的数字都被push进栈仍然没有找到这个数字,表明该序列不可能是一个pop序列。
基于前面的分析,我们可以写出如下的参考代码:
#include <stack>
/////////////////////////////////////////////////////////////////////////////
// Given a push order of a stack, determine whether an array is possible to
// be its corresponding pop order
// Input: pPush - an array of integers, the push order
// pPop - an array of integers, the pop order
// nLength - the length of pPush and pPop
// Output: If pPop is possible to be the pop order of pPush, return true.
// Otherwise return false
/////////////////////////////////////////////////////////////////////////////
bool IsPossiblePopOrder(const int*
pPush, const int* pPop, int nLength)
{
bool bPossible = false;
if(pPush && pPop && nLength > 0)
{
const int *pNextPush = pPush;
const int *pNextPop = pPop;
// ancillary stack
std::stack<int> stackData;
// check every integers in pPop
while(pNextPop - pPop < nLength)
{
// while the top of the ancillary stack is not the integer
// to be poped, try to push some integers into the stack
while(stackData.empty() || stackData.top() != *pNextPop)
{
// pNextPush == NULL means all integers have been
// pushed into the stack, can't push any longer
if(!pNextPush)
break;
stackData.push(*pNextPush);
// if there are integers left in pPush, move
// pNextPush forward, otherwise set it to be NULL
if(pNextPush - pPush < nLength - 1)
pNextPush ++;
else
pNextPush = NULL;
}
// After pushing, the top of stack is still not same as
// pPextPop, pPextPop is not in a pop sequence
// corresponding to pPush
if(stackData.top() != *pNextPop)
break;
// Check the next integer in pPop
stackData.pop();
pNextPop ++;
}
// if all integers in pPop have been check successfully,
// pPop is a pop sequence corresponding to pPush
if(stackData.empty() && pNextPop - pPop == nLength)
bPossible = true;
}
return bPossible;
}
测试程序:
#include<iostream>
#include<stack>
const int SIZE=5;
using namespace std;
bool judge(int Spush[],int Spop[])
{
stack<int> my_stack;
int iPush=0,iPop=0;
while(iPush<SIZE)
{
cout<<"push"<<Spush[iPush]<<endl;
my_stack.push(Spush[iPush]);
while(!my_stack.empty()&&iPop!=5&&my_stack.top()==Spop[iPop])
{
cout<<"Pop "<<Spop[iPop]<<endl;
iPop++;
my_stack.pop();
}
iPush++;
}
if(iPop==SIZE) return true;
else return false;
}
int main(void)
{
int Spush[SIZE],Spop[SIZE];
for(int i=0;i<SIZE;i++)
cin>>Spush[i];
for(int j=0;j<SIZE;j++)
cin>>Spop[i];
if(judge(Spush,Spop))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
system("pause");
return 0;
}
参考资料:<<剑指offer 作者:何海涛>>
相关文章推荐
- 程序员面试题精选100题(24)-栈的push、pop序列
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题精选100题(24)-栈的push、pop序列
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题精选100题(24)-栈的push、pop序列
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题100题第24题——判断是否是push、pop序列
- 100题_24 栈的push、pop序列
- 程序员面试题精选100题(06)-判断整数序列是不是二元查找树的后序遍历结果
- 程序员面试题精选100题(06)-判断整数序列是不是二元查找树的后序遍历结果
- 程序员面试题精选100题(26)-和为n连续正数序列
- 微软,Google面试题 (24) —— 栈的push、pop序列
- 程序员面试题精选100题(26)-和为n连续正数序列
- 程序员面试题精选100题(47)-数组中出现次数超过一半的数字[算法]
- 剑指Offer---面试题22:栈的push,pop序列问题
- 程序员面试题精选100题(13)-第一个只出现一次的字符[算法]
- 程序员面试题精选100题:6-10解题报告
- 程序员面试题精选100题(15)-含有指针成员的类的拷贝[C/C++/C#]
- 程序员面试题精选100题(52)-C++面试题(1)
- 程序员面试题精选100题(34)-数组中只出现一次的数字[算法]