用递归打印数字
2016-06-28 20:02
183 查看
题目
用递归的方法找到从1到最大的N位整数。解题
线性打印public List<Integer> numbersByRecursion(int n) { // write your code here List<Integer> result = new ArrayList<Integer>(); int Max = (int)Math.pow(10,n); for(int i=1;i< Max;i++) result.add(i); return result; }
递归看下面程序理解
public class Solution { /** * @param n: An integer. * return : An array storing 1 to the largest number with n digits. */ public List<Integer> numbersByRecursion(int n) { // write your code here List<Integer> result = new ArrayList<Integer>(); if(n<=0) return result; PrintN(n,result); return result; } public int PrintN(int n,List<Integer> res){ if(n==0){ return 1; } int base = PrintN(n-1,res); int size = res.size(); for(int i = 1;i<= 9;i++){ int subbase = base*i; res.add(subbase); for(int j= 0;j< size ;j++){ res.add(subbase+res.get(j)); } } return base*10; } }
分析待完善
相关文章推荐
- c语言swap(a,b)值交换的4种实现方法
- 《剑指offer》:[59]对称的二叉树
- 复选框与文字对齐的问题
- iOS开发 - 第03篇 - 多线程 - 01 - 多线程基础
- HTML5 新的 Input 类型
- VLC播放RTSP视频流(360浏览器可用)
- 杂记
- html 图像映射(一个图像多个连接)
- 块级元素文本垂直居中方法
- 图中连通块的个数:并查集
- IE8下对象数组的长度问题
- PyCharm 中文注释报错 SyntaxError: Non-ASCII character
- JavaScript绑定事件的方法[3种]
- android opengl jni中纹理贴图
- [bzoj3572][HNOI2014]世界树
- gcc编译器对宽字符的识别
- Jquery的各个Ajax函数,$.get(),$.post(),$.ajax(),$.getJSON()的用法
- 项目中二级指针的使用
- 文件和目录属性
- Solr - 主/从同步