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?

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

Runtime: 179ms.

 1 class Solution {
 2 public:
 3     /**
 4      * @param n: An integer.
 5      * return : An array storing 1 to the largest number with n digits.
 6      */
 7     vector<int> numbersByRecursion(int n) {
 8         // write your code here
 9         vector<int> result;
10         if (n <= 0) return result;
11
12         printNumbers(result, 0, n);
13         return result;
14     }
15
16 private:
17     void printNumbers(vector<int>& result, int depth, int n) {
18         if (depth >= n) return;
19
20         int copyNumber = result.size();
21         for (int i = 1; i <= 9; i++) {
22             int base = i * pow(10, depth);
23             result.push_back(base);
24             for(int j = 0; j < copyNumber; j++) {
25                 result.push_back(base + result[j]);
26             }
27         }
28         printNumbers(result, depth + 1, n);
29     }
30 };
时间: 2024-08-27 21:19:46

Print Numbers by Recursion的相关文章

Lintcode371 Print Numbers by Recursion solution 题解

[题目描述] 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

lintcode-medium-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 rec

[email&#160;protected] Sieve of Eratosthenes (素数筛选算法) &amp; Related Problem (Return two prime numbers )

Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,

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

[题目] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 例如:如果输入如下矩阵: 1            2            3            4 5            6            7            8 9            10          11           12 13          14          15           16 则依次打印出数字1, 2, 3, 4, 8, 12, 16, 15, 14

URAL 1506. Columns of Numbers(模拟啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1506 Every New Russian has to look through long columns of numbers for analyzing market trends and planning his investments. Psychologists assure that the longer is a column of numbers, the more diffi

python入门编程之基础

Python, 是一种面向对象.解释型计算机程序设计语言.Python语法简洁清晰,特色之一是强制用空白符作为语句缩进.Python的设计哲学是"优雅"."明确"."简单". Python是一门具有强类型(即变量类型是强制要求的).动态性.隐式类型(不需要做变量声明).大小写敏感(var和VAR代表了不同的变量)以及面向对象(一切皆为对象)等特点的编程语言. Python可用来干嘛?系统编程 :提供API(Application Programm

C++标准库 -- tuple

头文件:<tuple> 可访问属性: 无(用get方法来访问数据) 可访问方法: swap(tuple) 和另外一个tuple交换值 其他相关方法: swap(t1, t2) 交换两个tuple make_tuple(v1,v2..) 创建一个tuple get<?>(tuple) 访问数据 tie(v1, v2..) 创建由reference构成的tuple 例子: 例子1:构造tuple tuple<int, float, string> t0; tuple<

Python 列表推导、迭代器与生成器

1.列表推导 1 2 3 4 5 6 7 8 9 10 11 numbers = [i for i in range(10) if i % 2 == 0] print(numbers) seq = ["one", "two", "three"] for i, element in enumerate(seq):     print(i, element) def treatment(pos, element):     return ('%d:

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible