Print Numbers by Recursion
Print numbers from 1 to the largest number with N digits by recursion.
Notice
It's pretty easy to do recursion like:
recursion(i) {
if i > largest number:
return
results.add(i)
recursion(i + 1)
}
however this cost a lot of recursion memory as the recursion depth maybe very large. Can you do it in another way to recursive with at most N depth?
Given N = 1
, return [1,2,3,4,5,6,7,8,9]
.
Given N = 2
, return [1,2,3,4,5,6,7,8,9,10,11,12,...,99]
.
分析:
我们可以按“层”打印,什么意思呢?
第一层 1 - 9 = 1 to (pow(10, 1) - pow(10, 0))
第二层: 10 - 99 = 10 to (pow(10, 2) - pow(10, 1))
第三层: 100 - 999 = 100 to (pow(10, 3) - pow(10, 2))
...
看到规律了吧。
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) {
List<Integer> list = new ArrayList<Integer>();
if (n <= ) return list;
// start[0] refers to the start number for the current levevl.
// start[1] refers to the exponent.
int[] start = {, };
helper(n, list, start);
return list;
} public void helper(int n, List<Integer> list, int[] start) {
if (n == ) return;
for (int i = ; i <= (int)(Math.pow(, start[]) - Math.pow(, start[] - )); i++) {
list.add(start[]++);
}
start[]++;
helper(n - , list, start);
}
}
Print Numbers by Recursion的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 51. 顺时针打印矩阵[print matrix in clockwise direction]
[本文链接] http://www.cnblogs.com/hellogiser/p/print-matrix-in-clockwise-direction.html [题目] 输入一个矩阵,按照从外 ...
- 38.输出1到最大的N位数[Print 1 to max number of N bits]
[题目] 输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. [分析] 这是一道很有意思的题目.看起来很简单,其实里面却有不少的玄机. [常规思路 ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- [Swift] 随机数 | Random numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- 6 小时 Python 入门
6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- CF2.C
C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- DAY4-Python学习笔记
1.XML: 操作XML有两种方法:DOM和SAX DOM:把整个XML读入内存,解析为树,因此占用内存大,解析慢,优点是可以任意遍历树的节点 SAX:是流模式,边读边解析,占用内存小,解析快,缺点是 ...
- BZOJ3142 HNOI2013数列(组合数学)
考虑差分序列.每个差分序列的贡献是n-差分序列的和,即枚举首项.将式子拆开即可得到n*mk-1-Σi*cnt(i),cnt(i)为i在所有差分序列中的出现次数之和.显然每一个数出现次数是相同的,所以c ...
- 左连接,右连接和等值连接(left join,right join和inner join)
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- 一步步创建第一个Docker App —— 2. 创建 Docker化 主机
原文:https://docs.docker.com/engine/getstarted-voting-app/node-setup/ 部署voting app的第一步,是为集群节点(swarm no ...
- 【BZOJ4804】欧拉心算
Description 给定数字\(n\)(\(n\le 10^7\)),求: \[ \sum_{i=1}^n\sum_{j=1}^n\varphi(\gcd(i,j)) \] 多组数据输入,数据 ...
- 【codeforces 778C】 Peterson Polyglot
http://codeforces.com/problemset/problem/778/C (题目链接) 题意 给出一个字典树,问删掉哪一层以后,得到的字典树最小. Solution 直接对于每一层 ...
- 【bzoj3209】 花神的数论题
http://www.lydsy.com/JudgeOnline/problem.php?id=3209 (题目链接) 题意 ${sum(i)}$表示${i}$的二进制表示中${1}$的个数.求${\ ...
- keepalived回顾
Keepalived是lvs的扩展项目,因此它们之间具备良好的兼容性. 通过对服务器池对象的健康检查,实现对失效机器/服务的故障隔离: 负载均衡器之间的失败切换failover,通过VRRPv2 st ...
- 我们为什么要迁移PHP到HHVM
我们为什么要迁移PHP到HHVM 程序员日志 · 2014-12-26 18:33 简介 该调研是2013年10月份做的,目标是寻找更好的PHP引擎,来代替百度各产品线正在使用的PHP 5.2. 环境 ...
- 关于javaweb中图片的存储问题
图片上传到服务器,然后把上传路径保存到数据库,然后从数据库读出保存的路径显示到网站页面. 我们一般可以在CMS系统中将图片添加到图片服务器中(这个可以使用ftp来部署),然后图片上传到服务器后,在数据 ...