指定栈的进栈顺序,输出所有可能的出栈顺序

来源:互联网 发布:弱视网络训练软件 编辑:程序博客网 时间:2024/05/18 20:08
public static void stackOut(int[] nums,int inNum,int outNum,LinkedList<Integer> stack,ArrayList<Integer> output){if(inNum==nums.length&&outNum==nums.length){System.out.println(output);num++;}else{LinkedList<Integer> stack1=(LinkedList<Integer>)stack.clone();LinkedList<Integer> stack2=(LinkedList<Integer>)stack.clone();if(inNum<nums.length){stack1.push(nums[inNum]);stackOut(nums,inNum+1,outNum,stack1,output);stack1.pop();}ArrayList<Integer> output1=(ArrayList<Integer>)output.clone();if(inNum>outNum){output1.add(stack2.pop());stackOut(nums,inNum,outNum+1,stack2,output1);}}}

阅读全文
1 0
原创粉丝点击