http://www.lintcode.com/en/problem/print-numbers-by-recursion/
递归打印1 ~ n,n可能很大,要防止栈溢出
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) {
// write your code here
List<Integer> res = new ArrayList();
recursive(n, 0, res);
return res;
}
private void recursive(int n, int ans, List<Integer> res) {
if (n == 0) {
// 保证第一个0不出现
if (ans > 0) {
res.add(ans);
}
return;
}
for (int i = 0; i < 10; i++) {
recursive(n - 1, ans * 10 + i, res);
}
}
}