/****************************************************************************************
*题目:栈的压入、弹出序列
* 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。加上压入栈的所有数字均不相等。
* 例如序列1、2、 3、 4、 5是某栈的压栈序列,序列4、 5、 3、 2、 1是该栈的序列对应的一个弹出序列,但是4、 3、 5、 1、 2就不可能是该栈序列的弹出序列
*时间:2015年9月2日09:26:54
*文件:PopOrder.java
*作者:cutter_point
****************************************************************************************/
package bishi.Offer50.y2015.m09.d02;
import java.util.Stack;
public class PopOrder
{
/**
* 用来判断是否是我们的弹出序列
* @param pPush 压栈序列
* @param pPop 弹出序列
* @return
* @throws Exception
*/
public boolean isPopOrder(int pPush[], int pPop[]) throws Exception
{
if(pPush == null || pPop == null)
throw new Exception("序列为空");
if(pPush.length != pPop.length)
return false;
//建立一个辅助栈
Stack<Integer> outdata = new Stack<Integer>();
int pPushindex = 0, pPopindex = 0;
//我们循环遍历验证弹出序列
while(pPopindex < pPop.length)
{
//如果压栈数组在范围内
if(pPushindex < pPush.length)
{
//判断当前的压栈序列是否就是我们的出栈序列
if(pPush[pPushindex] != pPop[pPopindex])
{
//如果栈是空的
if(outdata.empty())
{
//如果栈为空,并且两数不等,那么压栈
outdata.push(pPush[pPushindex++]);
}//if
else
{
//如果栈不为空
if(outdata.peek() == pPop[pPopindex])
{
//如果和栈顶元素一样,那么就出栈
outdata.pop();
++pPopindex;
}//if
else
{
//如果和栈顶元素,不一样,那么就压栈
outdata.push(pPush[pPushindex++]);
}//else
}//else
}//if
else
{
//如果正好是相等的,那么就直接++
++pPushindex;
++pPopindex;
}//else
}//if
else
{
//压栈数组遍历完了
//如果栈是空的
if(outdata.empty())
{
//如果栈为空,压栈遍历完了,但是出栈序列还没完
break;
}//if
else
{
//如果栈不为空
if(outdata.peek() == pPop[pPopindex])
{
//如果和栈顶元素一样,那么就出栈
outdata.pop();
++pPopindex;
}//if
else
{
//如果和栈顶元素,不一样,那么就压栈
break;
}//else
}//else
}//else
}//while
//如果推出循环的时候,出栈索引不是到了最后那么就是失败
if(pPopindex < pPop.length)
return false;
return true;
}
public void test(String testName, int pPush[], int pPop[], boolean expected) throws Exception
{
if (testName != null)
System.out.println(testName);
//StackXuLie *sxl = new StackXuLie();
if (this.isPopOrder(pPush, pPop) == expected)
System.out.println("Passed.");
else
System.out.println("failed.");
}
public void Test1() throws Exception
{
int push[] = { 1, 2, 3, 4, 5 };
int pop[] = { 4, 5, 3, 2, 1 };
this.test("Test1", push, pop, true);
}
public void Test2() throws Exception
{
int push[] = { 1, 2, 3, 4, 5 };
int pop[] = { 3, 5, 4, 2, 1 };
this.test("Test2", push, pop, true);
}
public void Test3() throws Exception
{
int push[] = { 1, 2, 3, 4, 5 };
int pop[] = { 4, 3, 5, 1, 2 };
this.test("Test3", push, pop, false);
}
public void Test4() throws Exception
{
int push[] = { 1, 2, 3, 4, 5 };
int pop[] = { 3, 5, 4, 1, 2 };
this.test("Test4", push, pop, false);
}
public void Test5() throws Exception
{
int push[] = { 1 };
int pop[] = { 2 };
this.test("Test5", push, pop, false);
}
// push和pop序列只有一个数字
public void Test6() throws Exception
{
int push[] = { 1 };
int pop[] = { 1 };
this.test("Test6", push, pop, true);
}
public void Test7() throws Exception
{
this.test("Test7", null, null, true);
}
public static void main(String[] args) throws Exception
{
PopOrder po = new PopOrder();
po.Test1();
po.Test2();
po.Test3();
po.Test4();
po.Test5();
po.Test6();
po.Test7();
}
}
【笔试】39、栈的压入、弹出序列
最新推荐文章于 2021-10-20 00:09:13 发布