*/ public static boolean isPopOrder(int[] pushA, int[] popA) { if (pushA.length == 0 || popA.length == 0) { return false; } Stack<Integer> stack = new Stack<>(); //用于标识弹出序列的位置 int popIndex = 0; for (int i = 0; i < pushA.length; i++) { stack.push(pushA...
例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)题目链接: 栈的压入、弹出序列代码import java.util.Stack;/** * 标题:栈的压入、弹出序列 * 题目描述 * 输入两个整数序列...
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。 URL:https://leetcode....
好兄弟可以点赞并关注我的公众号“javaAnswer”,全部都是干货。 http://weixin.qq.com/r/MxywqEPElh7prQfL90kh (二维码自动识别)
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if(...
import java.util.*; public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { if(pushA == null || popA == null) return false; Stack<Integer> stack = new Stack<>(); int popIndex = 0; for(int i = 0; i < pushA.length; i++){ ...
import java.util.Stack; public class Main { public static void main(String[] args) { int[] push = new int[]{1, 2, 3, 4, 5}; int[] pop = new int[]{4, 5, 3, 1, 2}; System.out.println(check(push, pop)); } public static boolean check(int[] push, int[] pop) { ...
依次执行,最后辅助栈为空,如果不为空说明弹出序列不是该栈的弹出顺序。用java实现该思路。 importjava.util.ArrayList;importjava.util.Stack;publicclassSolution{publicbooleanIsPopOrder(int[]pushA,int[]popA){Stack<Integer>assistant=newStack<Integer>();intindex=0;for(inti=0;i<pushA.length;i++){assistan...
import java.util.Stack; public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { Stack stack = new Stack(); int in=0,out=0; while(in<pushA.length){ stack.push(pushA[in++]);//每次先进栈 //若栈顶元素与出栈顺序数组当前元素相等,则循环出栈 while(!stack.isEmpty(...
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。