Print numbers recursively

Keywords: Programming

371. Print numbers recursively

Find N-bit integers from 1 to the largest by recursive method.

Example

Give 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,..., 99].

Challenge

Complete it recursively, not circularly.

Matters needing attention

It's easy to recurse in the following way:

recursion(i) {
    if i > largest number:
        return
    results.add(i)
    recursion(i + 1)
}

However, this approach consumes a lot of recursive space, leading to stack overflow. Can you recurse in other ways so that the depth of recursion is at most N?

class Solution:
    """
    @param n: An integer
    @return: An array storing 1 to the largest number with n digits.
    """
    def numbersByRecursion(self, n):
        # write your code here
        if n<1:
            return []
        largest=pow(10,n)
        res=[]
        for i in range(1,largest):
            res.append(i)
        return res
        

########################################################
class Solution {
public:
    /**
     * @param n: An integer
     * @return: An array storing 1 to the largest number with n digits.
     */
    vector<int> numbersByRecursion(int n) {
        // write your code here
        vector<int> res;
        if(n<1) return res;
        long R=1;
        for(int i=0;i<n;i++)
        {
            R*=10;
        }
        numbersByRecursion(res,R,1);
        return res;
        
    }
    
private:
    void numbersByRecursion(vector<int>& res,long R,long number)
    {
        if(R<=number) return ;
        res.push_back(number);
        numbersByRecursion(res,R,number+1);
    }
};
####################################################################
class Solution {
public:
    /**
     * @param n: An integer
     * @return: An array storing 1 to the largest number with n digits.
     */
    vector<int> numbersByRecursion(int n) {
        // write your code here
        vector<int> res;
        if(n<1) return res;
        numbersByRecursion(n,0,res);
        return res;
        
    }
    
private:
    void numbersByRecursion(int n,int ans,vector<int>& res)
    {
        if(n==0)
        {
            if(ans>0)
            {
                res.push_back(ans);
            }
            return ;
        }
        for(int i=0;i<10;i++)
        {
            numbersByRecursion(n-1,ans*10+i,res);
        }
    }
};
##########################################################

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
        ArrayList<Integer> res=new ArrayList();
        if(n<1)
        {
            return res;
        }
         
        numbersByRecursion(n,0,res);
        return res;
        
    }
    public void numbersByRecursion(int n,int ans,ArrayList<Integer> res)
    {
        if(n==0)
        {
            if(ans>0)
            {
                res.add(ans);
            }
            return ;
        }
        for(int i=0;i<10;i++)
        {
            numbersByRecursion(n-1,ans*10+i,res);
        }
       
    }
}

 

 

Posted by imperium2335 on Wed, 23 Jan 2019 23:45:13 -0800