Print numbers from 1 to the largest number with N digits by recursion.

Notice

It's pretty easy to do recursion like:

  1. recursion(i) {
  2. if i > largest number:
  3. return
  4. results.add(i)
  5. recursion(i + 1)
  6. }

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?

Have you met this question in a real interview?

Yes
Example

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))

...

看到规律了吧。

  1. public class Solution {
  2. /**
  3. * @param n: An integer.
  4. * return : An array storing 1 to the largest number with n digits.
  5. */
  6. public List<Integer> numbersByRecursion(int n) {
  7. List<Integer> list = new ArrayList<Integer>();
  8. if (n <= ) return list;
  9. // start[0] refers to the start number for the current levevl.
  10. // start[1] refers to the exponent.
  11. int[] start = {, };
  12. helper(n, list, start);
  13. return list;
  14. }
  15.  
  16. public void helper(int n, List<Integer> list, int[] start) {
  17. if (n == ) return;
  18. for (int i = ; i <= (int)(Math.pow(, start[]) - Math.pow(, start[] - )); i++) {
  19. list.add(start[]++);
  20. }
  21. start[]++;
  22. helper(n - , list, start);
  23. }
  24. }

Print Numbers by Recursion的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. 51. 顺时针打印矩阵[print matrix in clockwise direction]

    [本文链接] http://www.cnblogs.com/hellogiser/p/print-matrix-in-clockwise-direction.html [题目] 输入一个矩阵,按照从外 ...

  3. 38.输出1到最大的N位数[Print 1 to max number of N bits]

    [题目] 输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. [分析] 这是一道很有意思的题目.看起来很简单,其实里面却有不少的玄机. [常规思路 ...

  4. 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 ...

  5. [Swift] 随机数 | Random numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  7. 6 小时 Python 入门

    6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...

  8. 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 ...

  9. CF2.C

    C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. 在tensorflow环境下安装matplotlib

    在运行程序时,报错ImportError: No module named 'matplotlib',如图.经网上查询发现是没有安装matplotlib 因此记录一下在tensorflow环境下安装m ...

  2. MT【149】和式变形

    (2018浙江省赛14题)将$2n(n\ge2)$个不同的整数分成两组$a_1,a_2,\cdots,a_n;b_1,b_2,\cdots,b_n$.证明:$\sum\limits_{1\le i\l ...

  3. 【hihocoder编程练习赛9】闰秒

    题目链接 #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h&g ...

  4. 【BZOJ1914】数三角形(组合数,极角排序)

    [BZOJ1914]数三角形(组合数,极角排序) 题面 BZOJ权限题 良心洛谷 题解 这种姿势很吼啊,表示计算几何啥的一窍不通来着. 题目就是这样,正难则反,所以我们不考虑过原点的三角形, 反过来, ...

  5. phpredis用法笔记

    项目中用到redis集群, 发现phpredis对集群,分布式是有支持的.翻译下相关资料备用. redis扩展地址:https://github.com/phpredis/phpredis, 看到如下 ...

  6. 【左偏树】【P3261】 [JLOI2015]城池攻占

    Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其 ...

  7. golang简单实现二叉树的数据添加和遍历

    代码实现 package tree import "fmt" type Node struct { elem interface{} left, right *Node } typ ...

  8. C++中的空类,编译器默认可以产生哪些成员函数

    C++中的空类,编译器默认可以产生哪些成员函数 C++中创建一个空类:class Empty {};默认会生成4个函数,其函数的原型如下: public: Empty() { ... } Empty( ...

  9. Python【zip-map-filter】三个内置函数

    print("============内置函数:zip===========")l2 = ['a','b','c','e','f','g']l3 = [1,2,3]L4=['A', ...

  10. 第10章-Vue.js 项目实战

    一.本节内容 掌握项目环境中路由的配置方法 ***** 熟练掌握编写单文件组件的编写 *** 能够使用swiper.js进行轮播图组件的封装 能够使用axios进行数据请求 二.webpack项目的目 ...