zoukankan      html  css  js  c++  java
  • 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位数,就如同下面一样,这个很明显是找规律了。。。

  • 相关阅读:
    Json schema前奏 关于JSON
    笔试题:能被1~10同时整除的最小整数是2520,问能被1~20同时整除的最小整数是多少?
    CentOS7 安装 Docker、最佳Docker学习文档
    2019年4399暑期实习算法题2,迷宫路径条数
    2019vivo秋招提前批笔试题第3题
    python内存机制与垃圾回收、调优手段
    N皇后问题的python实现
    一行代码判断一个数是否是2的整数次方
    在O(1)的时间内删除链表节点
    打印从1到n位数的最大值
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4944831.html
Copyright © 2011-2022 走看看