• lintcode:递归打印数字


    题目

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

    样例

    给出 N = 1, 返回[1,2,3,4,5,6,7,8,9].

    给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10,11,...,99].

    注意

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

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

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

    挑战

    用递归完成,而非循环的方式

    解题

    非递归最简单了,先求出最大的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
            int N = 1;
            for(int i = 1;i<=n;i++){
                N = N*10;
            }
            N = N - 1;
            List<Integer> result = new ArrayList<Integer>();
            for(int i =1;i<= N ;i++){
                result.add(i);
            }
            return result;
        }
    }
    Java Code

    总耗时: 1629 ms

    class Solution:
        # @param n: An integer.
        # return : A list of integer storing 1 to the largest number with n digits.
        def numbersByRecursion(self, n):
            # write your code here
            N = 1
            for i in range(n):
                N *=10
            result = []
            for i in range(1,N):
                result.append(i)
            return result 
    Python Code

    总耗时: 674 ms

    给的递归方式,运行到74%RunTime Error

    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
            int N = 1;
            for(int i = 1;i<=n;i++){
                N = N*10;
            }
            N = N - 1;
            List<Integer> result = new ArrayList<Integer>();
            getPrint(1,N,result);
            return result;
        }
        public void getPrint(int i,int N,List<Integer> result ){
            if(i>N)
                return ;
            result.add(i);
            getPrint(i+1,N,result);
        }
    }
    Java Code

    参考程序来源

        public int PrintN(int n,List<Integer> res){
            if(n==0){
                return 1;
            }
            int base = PrintN(n-1,res);
            int size = res.size();
            for(int i = 1;i<= 9;i++){
                int subbase = base*i;
                res.add(subbase);
                for(int j= 0;j< size ;j++){
                    res.add(subbase+res.get(j));
                }
            }
            return base*10;
        }

    上面是关键程序

    在求 n-1位数到n位数的时候,先求n-2位数到n-1位数,就如同下面一样,这个很明显是找规律了。。。

  • 相关阅读:
    linux定时器
    TIMESTAMP和DATETIME的区别
    Linux进程或线程绑定到CPU
    C++学习笔记
    磁盘扇区校验和
    docker安装mysql,并配置部分表同步
    docker 安装tomcat
    多tomcat 同一个浏览器 多个项目 会导致session覆盖
    Ubuntu 安装 NodeJS
    Ubuntu 下安装 Arduino IDE
  • 原文地址:https://www.cnblogs.com/theskulls/p/4944831.html
Copyright © 2020-2023  润新知