首页 > 学院 > 开发设计 > 正文

剑指offer经典编程(十三)

2019-11-08 02:32:26
字体:
来源:转载
供稿:网友

栈的压入、弹出序列

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

import java.util.*;public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { if(pushA.length==0||popA.length==0||popA.length!=pushA.length) return false; Stack stack = new Stack(); int len = pushA.length; int topIndex = 0; for (int i=0;i<len;i++){ stack.push(pushA[i]); while (topIndex<len && (Integer)stack.peek() == popA[topIndex]){ stack.pop(); topIndex++; } } return stack.empty()?true:false; }}
上一篇:A+B Problem

下一篇:死锁的产生

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表