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 the recursion depth maybe very large. Can you do it in another way to recursive with at most N depth?

用递归的方法找到从1到最大的N位整数。

【注】用下面这种方式去递归其实很容易:

recursion(i) {

if i > largest number:

return

results.add(i)

recursion(i + 1)

}

但是这种方式会耗费很多的递归空间,导致堆栈溢出。你能够用其他的方式来递归使得递归的深度最多只有 N 层么?

【题目链接】

www.lintcode.com/en/problem/print-numbers-by-recursion/

【题目解析】

从小至大打印 N 位的数列,正如题目中所提供的recursion(i), 解法简单粗暴,但问题在于 N 稍微大一点时栈就溢出了,因为递归深度太深了。能联想到的方法大概有两种,一种是用排列组合的思想去解释,把0~9当成十个不同的数(字符串表示),塞到 N 个坑位中,这个用DFS来解应该是可行的;另一个则是使用数学方法,依次递归递推,比如 N=2 可由 N=1递归而来,具体方法则是乘10进位加法。题中明确要求递归深度最大不超过 N, 故DFS方法比较危险。

【参考答案】

www.jiuzhang.com/solutions/print-numbers-by-recursion/

原文地址:https://www.cnblogs.com/qiangqingci/p/8727126.html

时间: 2024-10-13 23:49:28

Lintcode371 Print Numbers by Recursion solution 题解的相关文章

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

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

Lintcode18 Subsets II solution 题解

[题目描述] Given a list of numbers that may has duplicate numbers, return all possible subsets Notice:Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subsets. 给

Lintcode31 Partition Array solution题解

[题目描述] Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:All elements < k are moved to the left;All elements >= k are moved to the right;Return the partitioning index, i.e the fir

Lintcode20 Dices Sum solution 题解

[题目描述] Throw n dices, the sum of the dices' faces is S. Given n, find the all possible value of S along with its probability. Notice:You do not care about the accuracy of the result, we will help you to output results. 扔 n 个骰子,向上面的数字之和为 S.给定 Given n,

Lintcode17 Subsets solution 题解

[题目描述] Given a set of distinct integers, return all possible subsets. Notice:Elements in a subset must be in non-descending order;The solution set must not contain duplicate subsets. 给定一个含不同整数的集合,返回其所有的子集 注意:子集中的元素排列必须是非降序的,解集必须不包含重复的子集 [题目链接] http:/

Lintcode1 A+B Problem solution 题解

[题目描述] Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Notice:There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and

Lintcode15 Permutations solution 题解

[题目描述] Given a list of numbers, return all possible permutations. Notice:You can assume that there is no duplicate numbers in the list. 给定一个数字列表,返回其所有可能的排列. 注意:你可以假设没有重复数字. [题目链接] http://www.lintcode.com/en/problem/permutations/ [题目解析] 遇到这种问题,很显然,第一个

Lintcode16 Permutations II solution 题解

[题目描述] Given a list of numbers with duplicate number in it. Find all unique permutations. 给出一个具有重复数字的列表,找出列表所有不同的排列. [题目链接] http://www.lintcode.com/en/problem/permutations-ii/ [题目解析] 跟 Permutations的解法一样,就是要考虑"去重".先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否