栈的push、pop序列 【微软面试100题 第二十九题】
2014-10-30 22:03
609 查看
题目要求:
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1、2、3、4、5是某栈的压栈序列,序列4、5、3、2、1是该压栈序列对应的一个弹出序列,单4、3、5、1、2就不可能是该压栈序列的弹出序列。
参考资料:剑指offer第22题。
题目分析:
如果下一个弹出的数字刚好是栈顶数字,那么直接弹出。如果下个弹出的数字不在栈顶,我们把压栈序列中还没有入栈的数字艳茹辅助栈,知道把下一个需要弹出的数字压入栈顶为止。如果所有的数字都压入了栈了仍然没有找到下一个弹出的数字,那么该序列不可能是一个弹出序列。
下面通过下图来讲解一下具体过程:
[b]
View Code
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1、2、3、4、5是某栈的压栈序列,序列4、5、3、2、1是该压栈序列对应的一个弹出序列,单4、3、5、1、2就不可能是该压栈序列的弹出序列。
参考资料:剑指offer第22题。
题目分析:
如果下一个弹出的数字刚好是栈顶数字,那么直接弹出。如果下个弹出的数字不在栈顶,我们把压栈序列中还没有入栈的数字艳茹辅助栈,知道把下一个需要弹出的数字压入栈顶为止。如果所有的数字都压入了栈了仍然没有找到下一个弹出的数字,那么该序列不可能是一个弹出序列。
下面通过下图来讲解一下具体过程:
[b]
// 《剑指Offer——名企面试官精讲典型编程题》代码 // 著作权所有者:何海涛 #include <iostream> #include <stack> using namespace std; bool IsPopOrder(const int* pPush, const int* pPop, int nLength) { bool bPossible = false; if(pPush != NULL && pPop != NULL && nLength > 0) { const int* pNextPush = pPush; const int* pNextPop = pPop; std::stack<int> stackData; while(pNextPop - pPop < nLength) { // 当辅助栈的栈顶元素不是要弹出的元素 // 先压入一些数字入栈 while(stackData.empty() || stackData.top() != *pNextPop) { // 如果所有数字都压入辅助栈了,退出循环 if(pNextPush - pPush == nLength) break; stackData.push(*pNextPush); pNextPush ++; } if(stackData.top() != *pNextPop) break; stackData.pop(); pNextPop ++; } if(stackData.empty() && pNextPop - pPop == nLength) bPossible = true; } return bPossible; } // ====================测试代码==================== void Test(char* testName, const int* pPush, const int* pPop, int nLength, bool expected) { if(testName != NULL) printf("%s begins: ", testName); if(IsPopOrder(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] = {4,3,5,1,2}; Test("Test2", 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(void) { Test1(); Test2(); Test5(); Test6(); Test7(); return 0; }
View Code
相关文章推荐
- 栈的push、pop序列 【微软面试100题 第二十九题】
- 微软面试100题-栈的push-pop序列问题
- 微软面试100题 之24栈的push、pop序列
- 微软面试100题之29题:栈的push、pop 序列
- 【100题】第二十九 栈的push、pop序列
- 程序员面试题精选100题(24)-栈的push、pop序列
- 二叉搜索树的后序遍历序列 【微软面试100题 第九题】
- 最长递减子序列 【微软面试100题 第四十七题】
- 和为n连续正数序列 【微软面试100题 第五十一题】
- 最长递减子序列 【微软面试100题 第四十七题】
- 程序员面试题精选100题(24)-栈的push、pop序列
- 微软100题40题-设计一个栈结构,满足一下条件:min,push,pop操作的时间复杂度为O(1)。
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题100题第24题——判断是否是push、pop序列
- 程序员面试题100题第24题——判断是否是push、pop序列
- 微软面试100之9 判断整数序列是不是二元查找树的后序遍历结果
- 微软等数据结构+算法面试100题(37)-- 和为n 连续正数序列
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- 程序员面试题精选100题(24)-栈的push、pop序列
- 微软,Google面试题 (24) —— 栈的push、pop序列