微软面试100题-66
来源:互联网 发布:天天回程车软件 编辑:程序博客网 时间:2024/05/16 15:39
66.颠倒栈(栈)。
题目:用递归颠倒一个栈。例如输入栈{1, 2, 3, 4, 5},1在栈顶。
颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。
package com.algo.ms;import java.util.Stack;public class ReverseStack66 {public void reverse(Stack res){if(!res.empty()) {int ele = (int) res.pop();this.reverse(res);this.addToBottom(res, ele);}}public void addToBottom(Stack res, int ele){if(res.empty()){res.push(ele); } else { int top = (int) res.pop();this.addToBottom(res, ele);res.push(top); }}public static void main(String[] args) {// TODO Auto-generated method stubStack res = new Stack();res.push(1);res.push(2);res.push(3);res.push(4);res.push(5);ReverseStack66 stack = new ReverseStack66();stack.reverse(res);System.out.println(res.peek());}}
0 0
- 微软面试100题-66
- 微软面试100题
- 微软面试100题
- 微软面试100题
- 微软面试100题
- 微软面试100题
- 面试宝典--[微软面试100题]
- [转载]微软面试100题
- 微软面试100题链接
- 微软面试100题整理
- [微软面试100题] memmove
- 微软面试100题总结
- 微软面试100题 - 53
- 微软面试100题-60
- 微软面试100题-65
- 微软面试100题-64
- 微软面试100题-68
- 微软面试100题-69
- ldpack工作日记-2016/5/11&12
- Android开发:自定义Spinner适配器
- App引导页面代码示例
- strcpy函数实
- 全局捕获异常
- 微软面试100题-66
- poj 2586 Y2K Accounting Bug
- 使用HTML + CSS实现圆圈背景下的数字
- 大二实习投简历小记
- AOJ-737 关于最大公约数的疑惑
- Js apply方法详解
- mxgraph进阶(三)Web绘图——mxGraph项目实战(精华篇)
- 实践 HTML
- JAVA005-数组