多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ import java.util.Stack; /** * 只使用栈和递归逆序一个栈 */ public class Algorithm02 { public static void main(String[] args) { StackUtil stack = new StackUtil(); Stack<Integer> nums = new Stack<>(); nums.push(1); nums.push(2); nums.push(3); nums.push(4); nums.push(5); stack.reverse(nums); Integer result = null; while((result = nums.peek()) != null){ System.out.println(nums.pop()); } } } class StackUtil { private Integer getMin(Stack<Integer> stack) { Integer result = stack.pop(); if (stack.isEmpty()) { return result; } else { Integer minNum = getMin(stack); stack.push(result); return minNum; } } public void reverse(Stack stack) { if (stack != null && !stack.isEmpty()) { Integer result = getMin(stack); reverse(stack); stack.push(result); } } } ~~~