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 ...
随机推荐
- matlab gradient 和 prctile
介绍两个matlab小函数: 1.gradient 借用别人的例子:例:>> x=[6,9,3,4,0;5,4,1,2,5;6,7,7,8,0;7,8,9,10,0]x = 6 ...
- JAVA LOG4J使用方法
首先,需要在项目中导入log4j使用的JAR包,导入结果如下图: 菜单:Build Path->Configure Build Path->Add Extern Jars 导入JAR包后, ...
- python参数传递方式
原文地址:http://www.cnblogs.com/zhaopengcheng/p/5492183.html python中一切皆对象,函数中参数传递的是对象的引用. 1在函数中改变变量指向的对象 ...
- DSU模板(树的启发式合并)
摘自Codeforces博客 With dsu on tree we can answer queries of this type: How many vertices in subtree of ...
- BZOJ 2426: [HAOI2010]工厂选址
2426: [HAOI2010]工厂选址 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 364 Solved: 248[Submit][Status ...
- Centos最小化安装后还需要安装的软件包收集
自己也是初学,只是记录一下自己使用的时候需要安装的包: 1. yum install -y make wget mlocate net-tools 2. yum install -y gcc open ...
- 【Asp.net入门07】第一个ASP.NET 应用程序-创建数据模型和存储库
1.理解概念 先理解一下两个概念. 模型 模型是指数据的结构类型,以及可调用的方法.对面向对象编程方法来说,其实就是类.模型类就是一个描述数据的类.只有把数据按一定方式描述出来,我们才能在程序中方便地 ...
- django中的认证与登录
认证登录 django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 authenticate(**credentials) 提供了用户认证,即验证用户名以及密码是否 ...
- c# 的一些基本操作或属性
http下载文件,不保存到服务器,直接使用浏览器下载 /// <summary> /// 根据url下载文件 /// </summary> /// <param name ...
- 安装解压版的mariadb
今天尝试了安装解压版的mariadb,在官网上https://downloads.mariadb.org/下载了5.5版本的mariadb的zip压缩包, 经过实践发现mariadb解压版安装与mys ...