当前位置:网站首页>Recursive structure
Recursive structure
2022-07-04 03:18:00 【hv102】
Recursive structure consists of two parts :
- Define recursive headers . solve : When not to call your own methods . If there is no head , Will fall into a dead cycle , That is, the end condition of recursion .
- Recursive body . solve : When do I need to call my own methods .
- Example : Use recursion to find n
public class Test22 { public static void main(String[ ] args) { long d1 = System.currentTimeMillis(); factorial(10); long d2 = System.currentTimeMillis(); System.out.printf(" Recursive time-consuming :"+(d2-d1)); // Time consuming :32ms } /** The way to find factorial */ static long factorial(int n){ if(n==1){// Recursive header return 1; }else{// Recursive body return n*factorial(n-1);//n! = n * (n-1)! } } }The execution result is shown in the figure :


The flaw of recursion
Simple algorithm is one of the advantages of recursion . But recursive calls take up a lot of system stack , Memory consumption , When there are many levels of recursive calls, the speed is much slower than the loop , So be careful when using recursion .
边栏推荐
- Leetcode51.n queen
- Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
- If you have just joined a new company, don't be fired because of your mistakes
- Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
- Recent learning fragmentation (14)
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- Development of digital collection trading platform development of digital collection platform
- Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
- Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
- Sword finger offer:55 - I. depth of binary tree
猜你喜欢

Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?

The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"

Unspeakable Prometheus monitoring practice

PHP database connection succeeded, but data cannot be inserted

Add token validation in swagger

Tsinghua University product: penalty gradient norm improves generalization of deep learning model

Teach you how to optimize SQL

Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex

Li Chuang EDA learning notes IX: layers
随机推荐
New year's first race, submit bug reward more!
Short math guide for latex by Michael downs
Network byte order
Unity knapsack system (code to center and exchange items)
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
Teach you how to optimize SQL
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
Development of digital collection trading platform development of digital collection platform
Easy to win insert sort
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
2006 translation
1day vulnerability pushback skills practice (3)
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
CSCI 2134
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
Unity controls the selection of the previous and next characters
Eh, the log time of MySQL server is less than 8h?
Handler source code analysis
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
Johnson–Lindenstrauss Lemma

