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位数,就如同下面一样,这个很明显是找规律了。。。
lintcode:递归打印数字的更多相关文章
- LintCode-371.用递归打印数字
用递归打印数字 用递归的方法找到从1到最大的N位整数. 注意事项 用下面这种方式去递归其实很容易: recursion(i) { if i > largest number: return re ...
- 【递归】数字三角形 简单dp
[递归]数字三角形 题目描述 对于大多数人来说,“我们是这么的正常,因此也就这么的平庸.”而天才总是与众不同的,所以当邪狼问修罗王:“老大,你蹲在那儿一动不动看了有半个小时了,蚂蚁有那么好看吗?” 修 ...
- 递归打印lua中的table
在lua中,table是比较常用的数据形式,有时候为了打印出里面的内容,需要做一些特殊处理. 废话不多讲,直接粘代码: print = release_print -- 递归打印table local ...
- 面试 15:顺时针从外往里打印数字(剑指 Offer 第 20 题)
面试 15:顺时针从外往里打印数字 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印每一个数字.例如输入: {{1,2,3}, {4,5,6}, {7,8,9}} 则依次打印数字为 1.2.3. ...
- UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)
题目描写叙述: 定义合法的括号序列例如以下: 1 空序列是一个合法的序列 2 假设S是合法的序列.则(S)和[S]也是合法的序列 3 假设A和B是合法的序列.则AB也是合法的序列 比如:以下的都是合法 ...
- 使用递归打印二叉树的左视图 java
使用递归打印二叉树的左视图 java package com.li.jinRiTouTiao; public class PrintLeftView { static class TreeNode{ ...
- DP(递归打印路径) UVA 662 Fast Food
题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...
- Shell脚本递归打印指定文件夹中全部文件夹文件
#!/bin/bash #递归打印当前文件夹下的全部文件夹文件. PRINTF() { ls $1 | while read line #一次读取每一行放到line变量中 do [ -d $1/$li ...
- Java n个线程轮流打印数字的问题
一. 实现两个线程.轮流打印出数字.例如以下: bThread --> 10 aThread --> 9 bThread --> 8 aThread --> 7 bThread ...
随机推荐
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- MotionEvent中getX()和getRawX()的区别
http://blog.csdn.net/ztp800201/article/details/17218067 public class Res extends Activity implements ...
- Android:自定义控件样式(Selector)
前言 在开发一个应用程序过程中不可避免的要去修改组件的样式,比如按钮.输入框等.现在就看下如何通过Seletor实现样式的自定义.先看下简单的效果对比
- linux下配置tomcat7 + solr4.9
一.安装准备 操作系统:CentOS 6.5 tomcat版本:apache-tomcat-7.0.54.tar.gz solr版本:solr-4.9.0.tgz 二.部署实施 安装tomcat:将t ...
- openerp 经典收藏 通过view实现字段的只读、隐藏操作(转载)
通过view实现字段的只读.隐藏操作 原文地址:http://cn.openerp.cn/view_groups/ 在OpenERP V7视图(ir.ui.view)多了一个非常有用的字段(group ...
- Sublime Text博客插件 --- iblog
iblog是一款 sublime 博客插件,目前只支持cnblog. 项目地址:https://github.com/iskeeter/iblog 功能介绍 新建和更新cnblog的博客 支持mark ...
- Ztack学习笔记(3)-系统启动分析
一 系统启动 //OSAL.cvoid osal_start_system( void ) { #if !defined ( ZBIT ) && !defined ( UBIT ) f ...
- 你的数据根本不够大,别老扯什么Hadoop了
本文原名"Don't use Hadoop when your data isn't that big ",出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯 ...
- ERROR 1005 (HY000): Can't create table'matrix.system_log' (errno: 150)
CREATE TABLE `user` (`id` bigint(32) NOT NULL AUTO_INCREMENT ,`name` varchar(32) CHARACTER SET utf8 ...
- Sql Server 常用自定义函数
-- select * from [dbo].[SplitToTable]('ADSF','|') -- 分解字符串 ALTER FUNCTION [dbo].[SplitToTable] ( @Sp ...