版权声明:本文为博主NJU_ChopinXBP原创文章,发表于CSDN,仅供交流学习使用,转载请私信或评论联系,未经博主允许不得转载。感谢您的评论与点赞。 https://blog.csdn.net/qq_20304723/article/details/81841730
2018.8.19
这道题实际上是考察对栈的压入弹出顺序的理解,做法不是太难,创建一个栈和两个指示器,一个入栈指示器,一个出栈指示器,分别从两个序列头开始向尾步进。在入栈指示器未进行完之前,当两指示器指示元素不同时,入栈序列元素持续入栈;当元素相同时,两个指示器同时前进一位(模拟一次入栈和出栈)。在入栈元素全部入完,即入栈指示器走完之后, 若栈顶元素与出栈指示器指示元素相同,则进行一次出栈,出栈指示器向前一位。
若出栈指示器能够走完序列,则说明序列是该栈的一个出栈序列。反之若中途出现栈顶元素与出栈指示器指示元素不同,则可以提前结束循环判定为否。
另外还有一道以前做过的变式版的题目——#数据结构与算法学习笔记#PTA7:出栈序列检验(C/C++),题目不同的地方是入栈序列是顺序的(难度降低了一些),但是栈有最大容量N(稍微又负责一点),所以不好说是升级版,只能说是一道变式吧。思路做到基本相同,感兴趣的朋友可以一试。
题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
Java实现:
/**
*
* @author ChopinXBP
* 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。
* 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
* (注意:这两个序列的长度是相等的)
*
*/
import java.util.Stack;
public class IsPopOrder_20 {
public static int[] pushA = { 1, 2, 3, 4, 5 };
public static int[] popA = { 4, 5, 2, 3, 1 };
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(IsPopOrder(pushA, popA));
}
public static boolean IsPopOrder(int[] pushA, int[] popA) {
if(pushA.length == 0 || popA.length == 0)
return false;
Stack<Integer> stack = new Stack<>();
int num = pushA.length;
int pushflag = 0;
int popflag = 0;
//若出栈指示器走完popA,则为说明弹出序列是该栈的弹出顺序。
while (popflag < num) {
if(pushflag < num){
//序列元素不同时,持续入栈
if(pushA[pushflag] != popA[popflag]){
stack.push(pushA[pushflag]);
}
//序列元素相同时,模拟一次入栈后出栈,即两个指示器同时前进一位
else{
popflag++;
}
}else{
//若栈顶元素相同,则出栈,不同则返回false
if(stack.peek() == popA[popflag]){
stack.pop();
popflag++;
}
else{
return false;
}
}
pushflag++;
}
return true;
}
//代码优化版
public boolean IsPopOrder2(int[] pushA, int[] popA) {
if (pushA.length == 0 || popA.length == 0)
return false;
Stack<Integer> s = new Stack<Integer>();
// 用于标识弹出序列的位置
int popIndex = 0;
for (int i = 0; i < pushA.length; i++) {
s.push(pushA[i]);
// 如果栈不为空,且栈顶元素等于弹出序列
while (!s.empty() && s.peek() == popA[popIndex]) {
// 出栈
s.pop();
// 弹出序列向后一位
popIndex++;
}
}
return s.empty();
}
}
C++实现示例:
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] = {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 _tmain(int argc, _TCHAR* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
return 0;
}
#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#